Jump to content

Archived

This topic is now archived and is closed to further replies.

Masna

JOIN

Recommended Posts

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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
[!--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.

Share this post


Link to post
Share on other sites
What if they weren't renamed, would it result in a MySQL error?

Share this post


Link to post
Share on other sites
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...

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.