Masna Posted March 1, 2006 Share Posted March 1, 2006 I need some help with JOIN. I've never used it before, and right now is the time. I have one table, where I store "contacts" for users. Then, I grab all these contact ids for the logged in user, and use IN() to get the information from a users table on these contacts. How can I grab all the information I need via one query, via JOIN? Thanks. Quote Link to comment Share on other sites More sharing options...
Masna Posted March 1, 2006 Author Share Posted March 1, 2006 Nevermind, JOIN was unecessary. I have another question though. If I have a query like this...[!--sql--][div class=\'sqltop\']SQL[/div][div class=\'sqlmain\'][!--sql1--][span style=\'color:blue;font-weight:bold\']SELECT[/span] table1.id, table2.id [color=green]FROM[/color] [color=orange]table1,[/color] table2 [!--sql2--][/div][!--sql3--]...and I use mysql_fetch_assoc() to get the ids, how will I be able to tell them apart, I guess you could say. Quote Link to comment Share on other sites More sharing options...
zq29 Posted March 1, 2006 Share Posted March 1, 2006 [!--quoteo(post=350792:date=Mar 1 2006, 09:52 PM:name=Masna)--][div class=\'quotetop\']QUOTE(Masna @ Mar 1 2006, 09:52 PM) [snapback]350792[/snapback][/div][div class=\'quotemain\'][!--quotec--]Nevermind, JOIN was unecessary. I have another question though. If I have a query like this...[!--sql--][div class=\'sqltop\']SQL[/div][div class=\'sqlmain\'][!--sql1--][span style=\'color:blue;font-weight:bold\']SELECT[/span] table1.id, table2.id [color=green]FROM[/color] [color=orange]table1,[/color] table2 [!--sql2--][/div][!--sql3--]...and I use mysql_fetch_assoc() to get the ids, how will I be able to tell them apart, I guess you could say.[/quote][!--sql--][div class=\'sqltop\']SQL[/div][div class=\'sqlmain\'][!--sql1--][span style=\'color:blue;font-weight:bold\']SELECT[/span] table1.id [color=green]AS[/color] id1, table2.id [color=green]AS[/color] id2 [color=green]FROM[/color] [color=orange]table1,[/color] table2 [!--sql2--][/div][!--sql3--]Then once you have passed the query through mysql_fetch_assoc() you'll have an array with the index's id1 and id2. Quote Link to comment Share on other sites More sharing options...
Masna Posted March 1, 2006 Author Share Posted March 1, 2006 What if they weren't renamed, would it result in a MySQL error? Quote Link to comment Share on other sites More sharing options...
zq29 Posted March 2, 2006 Share Posted March 2, 2006 Not sure, never tried it. You could try print_r(mysql_fetch_assoc($sql)); if you can do that, you may need to assign mysql_fetch_assoc() to a variable first... Quote Link to comment Share on other sites More sharing options...
wickning1 Posted March 2, 2006 Share Posted March 2, 2006 You are still doing a join. If you have two tables in the FROM part, you are doing a join. The comma is just another way to write INNER JOIN. Be sure you know what you are joining on and that you set up the WHERE conditions appropriately.To answer your other question, there will be no error when you have duplicate return names, and you can still access them by the numeric index if you use mysql_fetch_array(), but mysql_fetch_assoc() will not work properly. Setting an alias, like SemiApoc showed you, is the almost always best way to deal with it. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.