Jump to content


Photo

JOIN


  • Please log in to reply
5 replies to this topic

#1 Masna

Masna
  • Staff Alumni
  • Advanced Member
  • 288 posts
  • LocationNew York

Posted 01 March 2006 - 07:52 PM

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.
I like LoL.

#2 Masna

Masna
  • Staff Alumni
  • Advanced Member
  • 288 posts
  • LocationNew York

Posted 01 March 2006 - 09:52 PM

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 FROM table1, 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.
I like LoL.

#3 Kris

Kris
  • Staff Alumni
  • Advanced Member
  • 2,755 posts
  • LocationThe Internet

Posted 01 March 2006 - 10:14 PM

[!--quoteo(post=350792:date=Mar 1 2006, 09:52 PM:name=Masna)--][div class=\'quotetop\']QUOTE(Masna @ Mar 1 2006, 09:52 PM) View Post[/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 FROM table1, 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 AS id1, table2.id AS id2 FROM table1, 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.

#4 Masna

Masna
  • Staff Alumni
  • Advanced Member
  • 288 posts
  • LocationNew York

Posted 01 March 2006 - 11:56 PM

What if they weren't renamed, would it result in a MySQL error?
I like LoL.

#5 Kris

Kris
  • Staff Alumni
  • Advanced Member
  • 2,755 posts
  • LocationThe Internet

Posted 02 March 2006 - 07:34 PM

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...

#6 wickning1

wickning1
  • Members
  • PipPipPip
  • Advanced Member
  • 405 posts

Posted 02 March 2006 - 09:30 PM

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.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users