Jump to content

SQL Joins


SharkBait

Recommended Posts

I've never used a join in the sql query.

I am thinking that I really need to start doing it ;)

If I have two tables and wish to tie them together I would use a join correct? Currently I do the following:

[code]

SELECT * FROM table1, table2 WHERE table1.id = 3 AND table1.id = table2.id

[/code]

Would I be able to use a JOIN with that? I guess what I am really trying to do is select values from 3 seperate tables that all have some sort of correleation between them. IE an id field

Am I understanding how JOINs work? It mushes multple tables together into sort of a 'pseudo' table?



Link to comment
Share on other sites

If you're just starting to work with JOINs, I suggest that you NEVER use the comma operator -- trust me. I can give you a laundry list of reasons why it's bad.

Nonetheless, you're correct -- JOINs will accomplish the task you have outlined:

[code]SELECT * FROM table1
JOIN table2 ON ( table1.id = table2.id )
WHERE table1.id = 3[/code]

Not only does this make it more explicit, but it makes it much easier to change at a later date.
Link to comment
Share on other sites

[!--quoteo(post=380687:date=Jun 6 2006, 10:34 AM:name=fenway)--][div class=\'quotetop\']QUOTE(fenway @ Jun 6 2006, 10:34 AM) [snapback]380687[/snapback][/div][div class=\'quotemain\'][!--quotec--]
If you're just starting to work with JOINs, I suggest that you NEVER use the comma operator -- trust me. I can give you a laundry list of reasons why it's bad.

Nonetheless, you're correct -- JOINs will accomplish the task you have outlined:

[code]SELECT * FROM table1
JOIN table2 ON ( table1.id = table2.id )
WHERE table1.id = 3[/code]

Not only does this make it more explicit, but it makes it much easier to change at a later date.
[/quote]

So when you mean I should not use the comma operator, I shouldn't do SELECT * FROM table1,table2.table3

So I should always use JOINs?
Link to comment
Share on other sites

[!--quoteo(post=381848:date=Jun 9 2006, 10:25 AM:name=SharkBait)--][div class=\'quotetop\']QUOTE(SharkBait @ Jun 9 2006, 10:25 AM) [snapback]381848[/snapback][/div][div class=\'quotemain\'][!--quotec--]
So when you mean I should not use the comma operator, I shouldn't do SELECT * FROM table1,table2.table3

So I should always use JOINs?
[/quote]
That's right -- commas are short-hand. Not only do they force you to dump the join conditions in the where clause, which makes for many mistakes and clutter, but as of MySQL 5, they have different precedence that JOIN, which is yet another nightmare. Stick with JOINs.
Link to comment
Share on other sites

[!--quoteo(post=382188:date=Jun 10 2006, 05:43 AM:name=Barand)--][div class=\'quotetop\']QUOTE(Barand @ Jun 10 2006, 05:43 AM) [snapback]382188[/snapback][/div][div class=\'quotemain\'][!--quotec--]
Also, queries using the "FROM A JOIN B ON ..." execute faster than those using the "FROM A, B WHERE .." syntax
[/quote]
Really? I mean, I would tend to agree with you, but doesn't the optimizer do a fairly good job? That is, the EXPLAINs are often similar.
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.