join or union?

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
bytte
Forum Commoner
Posts: 75
Joined: Sun Nov 23, 2003 8:20 am
Location: Belgium

join or union?

Post 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;
		}
	}
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

your query will link together all combinations of ID and naam.. if the field names aren't ambiguous. Is this the intention?
bytte
Forum Commoner
Posts: 75
Joined: Sun Nov 23, 2003 8:20 am
Location: Belgium

Post 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
Last edited by bytte on Fri Jan 28, 2005 9:17 am, edited 1 time in total.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

union.
bytte
Forum Commoner
Posts: 75
Joined: Sun Nov 23, 2003 8:20 am
Location: Belgium

Post 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?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

looks correct..
bytte
Forum Commoner
Posts: 75
Joined: Sun Nov 23, 2003 8:20 am
Location: Belgium

Post by bytte »

Thanks a lot feyd. Again. :)
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

just noticed you don't increment $i...
bytte
Forum Commoner
Posts: 75
Joined: Sun Nov 23, 2003 8:20 am
Location: Belgium

Post by bytte »

yeah, i noticed it as well... when my browser froze 8)
Post Reply