Shorthand for MySQL JOIN

Written by on September 5, 2008 in General - 3 Comments

Hey guys, i am currently finishing my degree at collage and do not have a lot of time to write as you can see. Last month was short on post and i believe that this month will also be short, although i have already long list of topics i want to write about. Anyway i managed to find some time so here something interesting i found recently.

If you are like me then you probably go by the rule: less is more. I prefer to less code because it usually make applications more transparent and easy to review code later. So here is is shorthand for MySQL joins i found recently on some blog (sorry do not remember where), oh and by the way i assume you know how to use joins :) .

Usually if you want to JOIN two tables you would/could write such query:

SELECT * FROM table1 AS t1 LEFT JOIN table2 AS t2 
ON t1.id=t2.id AND t1.color=t2.color;

Not many MySQL developers know about it, or maybe i am the one who do not know about it but the same query can be written with less code:

SELECT * FROM table1 LEFT JOIN table2 USING (id, color);

Neet right? Arguments for USING() function is a list of columns to use for join statement, well i guess that’s, all there is nothing really to explain here, it is best to just compere those two statements.

That’s it for now, but one thing i have to mention i left this blog for like two or three weeks without a post and i guess spammers thought it is already abondened and within this three weeks there was over 3000 spam comments made, i do not think i will be going thru all this, i will just delete all, so if you left me an interesting comment and i will delete then … SORRY.

Update.

Actually i went thru all this, first i went to the phpMyAdmin and deleted over 2500 spam messages with only three queries ;> so there was only 300 comments left. So i manually seperated spam from real comments, only to find out that … there was only ONE real comment … that sucks :].

About the Author

Greg Winiarski is a freelance PHP and JavaScript programmer. He specializes in web applications and WordPress development.

3 Comments on "Shorthand for MySQL JOIN"

  1. ghprod October 10, 2008 at 12:42 am ·

    Wow … simple and easy :)

    thnx for this guide

  2. Apryl Hilvers April 25, 2010 at 8:27 pm ·

    I enjoy your blog here, thank you so much you have helped me out greatly :) love lots.

  3. Alex October 14, 2010 at 2:49 pm ·

    Hey, I recently remodelled my whole db and took the oppertunity to use the same foreign key names as their respective primary keys which allowed me to use this method throughout almost all my query joins, its so much cleaner now.

    Thanks for the heads up.

Leave a Comment