Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Output only displays value if in both tables

Status
Not open for further replies.

tarja311

Programmer
Jan 23, 2007
7
US
Hello all. I hope you can help me.

I have two tables, Table1 and Table2. When i submit a name from my form ( eg : jimmy ), 'jimmy' is inserted into both tables. Now this part is fine. In the output, it displays the data from both tables ( output: jimmy - jimmy )

The problem i am having lies in my sql statement i believe.

Basically, the way i have it now, it says, if 'jimmy' is in Table1 AND in Table2, output the results, otherwise forget it!

It does not output anything if 'jimmy' is in NOT in Table1 but IS in Table2, or vice versa.

My code :

Code:
$result = mysql_query("SELECT". 
				     "`TABLE1`.username,".
				     "`TABLE2`.username ".	
			FROM".
				     "`TABLE1`,".
				     "`TABLE2`".
			"WHERE".
				     "`TABLE1`.username = `TABLE2`.username ".
		"") or die(mysql_error());

Hopefully someone can guide me in the right direction.

thanks

-- tarja
 
Code:
SELECT TABLE1.username
     , TABLE2.username
  FROM TABLE1
[b]LEFT OUTER[/b]
  JOIN TABLE2
    ON TABLE1.username = TABLE2.username
[b]UNION ALL[/b]    
SELECT TABLE1.username
     , TABLE2.username
  FROM TABLE1
[b]RIGHT OUTER[/b]
  JOIN TABLE2
    ON TABLE1.username = TABLE2.username

r937.com | rudy.ca
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top