Page 1 of 1

join or union?

Posted: Fri Jan 28, 2005 9:03 am
by bytte
or how does it work. i've been searching and searching.
I basically need the output for a query like this (but this one doesn't exist of course):

Code: Select all

SELECT
     ID,
     naam
FROM
     menu_een, 
     menu_twee
How do I do this?


This is the function I'm using:

Code: Select all

function returnAllMenuItems() {
		$sql = "SELECT
					ID,
					naam
				FROM
					menu_een,
 menu_twee
				
				";
		
		$result = mysql_query($sql);
		
		
		if (!$result) {
			return FALSE;
		} else {
			$i = 0;
			while ($menu = mysql_fetch_array($result)) {
				$returnї$i]ї'ID']	= $menuї'ID'];
				$returnї$i]ї'naam'] = $menuї'naam'];
			}
			return $return;
		}
	}

Posted: Fri Jan 28, 2005 9:08 am
by feyd
your query will link together all combinations of ID and naam.. if the field names aren't ambiguous. Is this the intention?

Posted: Fri Jan 28, 2005 9:15 am
by bytte
basically I need to return all values that are stored in the field "naam" for the tables "menu_een" and "menu_twee".
So if there's "HOME" in menu_een and "LINKS" and "FEATURES" in menu_twee, this query should return:
HOME, LINKS, FEATURES

that's all.

So this would be sufficient I guess:

Code: Select all

SELECT  
     naam 
FROM 
     menu_een, 
     menu_twee


Or, maybe it's more clear like this. I want these two queries in only one query:

Code: Select all

SELECT  
     naam 
FROM 
     menu_een

Code: Select all

SELECT  
     naam 
FROM 
     menu_twee

Posted: Fri Jan 28, 2005 9:17 am
by feyd
union.

Posted: Fri Jan 28, 2005 9:21 am
by bytte
Thanks, so this would be the function?

Code: Select all

function returnAllMenuItems() {
$sql = "
(SELECT
     ID,
     naam
FROM
     menu_een)
UNION
(SELECT
     ID,
     naam
FROM
     menu_twee)
				";
		
$result = mysql_query($sql);
		
if (!$result) {
	return FALSE;
} else {
	$i = 0;
	while ($menu = mysql_fetch_array($result)) {
		$returnї$i]ї'ID']= $menuї'ID'];
		$returnї$i]ї'naam'] = $menuї'naam'];
	}
	return $return;
}
}
Or how do I return the values correctly?

Posted: Fri Jan 28, 2005 9:22 am
by feyd
looks correct..

Posted: Fri Jan 28, 2005 9:28 am
by bytte
Thanks a lot feyd. Again. :)

Posted: Fri Jan 28, 2005 9:31 am
by feyd
just noticed you don't increment $i...

Posted: Fri Jan 28, 2005 9:45 am
by bytte
yeah, i noticed it as well... when my browser froze 8)