The Tricky.net

Mysql join
Mysql join

There are several types of joins available in MySQL. Here is a short introduction to some of them with examples to illustrate. (note it is valid for MySQL 5, not sure what about later versions)


Let's first introduce the example which will illustrate our document.


Assume we have two tables: a table about authors and one about books:


Author Books
id name
1 Anna
2 George
3 Sean
4 Mary
book_id author_id title
1 1 book 1
2 1 book 2
3 2 book 3
4 3 book 4

The three joins we are going to see is the standard join and left join. There are a few more, but these are the ones that are used most frequently.


Join

So we will do a standard 2-way join on our two tables, combining them on the author_id. The mySQL query looks like this:


SELECT * FROM Author, Books WHERE Author.id = Books.author_id;


After the join, we will obtain the following table:


id name book_id author_id title
1 Anna 1 1 book 1
1 Anna 2 1 book 2
2 George 3 2 book 3
3 Sean 4 3 book 4

Left Join

The left join is takes the left table as it is and joins the right table whenever there is a correspondance. In the case of the previous example, the SQL code looks like this:

SELECT * FROM Author LEFT JOIN Books ON Author.id = Books.author_id;

From the view of the syntax, you can see that instead of writing the join condition after the "where" keyword, we use "on" this time.

After the join, the table will look like this:


id name book_id author_id title
1 Anna 1 1 book 1
1 Anna 2 1 book 2
2 George 3 2 book 3
3 Sean 4 3 book 4
4 Mary NULL NULL NULL

Right join

The right join works in the same way as the left join, but takes the right table as first table and then joins the left table when there is a correspondance. The syntax is very similar to the left join syntax:

SELECT * FROM Author RIGHT JOIN Books ON Author.id = Books.author_id;

After the join, the table will look the same as the normal join, in this case.

id name book_id author_id title
1 Anna 1 1 book 1
1 Anna 2 1 book 2
2 George 3 2 book 3
3 Sean 4 3 book 4


Comments
Add New Search RSS
Nice blog
Tushar (59.184.180.xxx) 14-05-2012 08:42:58

you have explained all this tutorials very simplified manner..
Please add some
more topics..
Supra Muska Skytop discount
Supra Muska Skytop discount (59.60.114.xxx) 23-04-2012 10:37:05

link:http://www.supratksocietyoutlet.com/supra-tk -society-purple-patent-w
omens-shoes-onsale-p-110.h tml , link:http://www.supratksocietyoutlet.com/black-re d-womens-supra-tk-societ
y-suede-shoes-p-109.html , link:http://www.supratksocietyoutlet.com/supra-tk -society-white-perf-leat
her-shoes-womens-p-141.htm l , link:http://www.supratksocietyoutlet.com/2012-sup ra-owen-red-white-mesh-s
hoes-on-sale-p-240.html, link:http://www.supratksocietyoutlet.com/grey-whi te-supra-tk-society-wome
ns-skateboarding-shoes-p-1 26.html , link:http://www.supratksocietyoutlet.com/supra-tk -society-purple-perf-lea
ther-womens-footwear-p-122 .html , link:http://www.supratksocietyoutlet.com/black-bl ue-supra-muska-skytop-tr
ainers-wholesale-p-101.htm l, link:http://www.supratksocietyoutlet.com/supra-mu ska-skytop-grey-black-me
ns-shoes-sale-online-p-85. html, link:http://www.supratksocietyoutle...
jewelry
sherry (59.61.83.xxx) 26-03-2012 10:37:16

http://www.wholesalepandorabracelets.com/ Do you want to be a charming Women
this summer?Wholesale Pandora Bracelets is the best choic,they are pretty,High
quality,and multifarious,there always something just for you!
Buy Jordan shoes from China
sherry (59.61.83.xxx) 26-03-2012 10:36:24

http://www.cheapjordansneakers.net/air-jordan-1-i- retro-c-78" Cheap Air
Jordan 1 (I) Retro Sneakers Sale - 50%
OFF!
http://www.cheapjordansneakers.net/air-jordan -2-ii-retro-c-79" Cheap
Air Jordan 2 (II) Retro Sneakers Sale - 50%
OFF!href="http://www.cheapjordansneakers.net/a
ir-jordan-3-iii-retro-c-81" Cheap Air Jordan 3 (III) Retro Sneakers Sale -
50% OFF!
http://www.cheapjordansneakers.net/air-jordan -4-iv-retro-c-82"
Cheap Air Jordan 4 (IV) Retro Sneakers Sale 50% OFF,Free
shipping!
http://www.cheapjordansneakers.net/air-j ordan-5-v-retro-c-83"
Cheap Air Jordan 5 (V) Retro Sneakers Sale 50% OFF!Free
shipping!
http://www.cheapjordansneakers.net/air-j ordan-6-vi-retro-c-84"
Cheap Air Jordan 6 (VI) Retro Sneakers Sale 50% OFF! Free
Shipping!
http://www.cheapjordansneakers.net/ cheap jordan sneakers are cheap
and nice,come and have a look!
tiffany necklace (114.99.17.xxx) 16-03-2012 04:44:17

Please remember that link:http://www.uktiffany-online.comvintage watches have survived for many
decades by being treated Romance God created the brand since 1988, link:http://www.uktiffany-online.com/tiffany-co-n ecklaces.htmlthe
company, registered in 102 countries around the world omega link:http://www.uktiffany-online.com/tiffany-co-e arrings/Elsa-Peretti-Ope
n-Heart-Earrings-1-uk.html seamaster ladies watches,jewelry
brand,merchandise exports to 70 countries, is a
specialized manufacturer of watches and jewelry. Romance of God in
1998 to become a global brand,hire famous Swiss designer Wolfgang Jonsson
program development of brand identity company logos,uniform color.
Romance God omega seamaster quartz watches Korea Co., Ltd. has become
the leading companies in the industry, the reputation is well known
South Korean brand abroad. God watches Co.Ltd...
lai lai bhari
marathi (115.115.82.xxx) 14-12-2011 13:03:25

lai bahri
Arjit (124.247.229.xxx) 16-04-2010 10:29:47

gr8.....though I had some gud knowledge, JOIN always confused me.I often use
2-way join..but never left/right join. Thanx to this article, I'll start to use
it, gr8 article, hope to see such articles often in this site
Write comment
Name:
Email:
 
Title:
:angry::0:confused::cheer:B):evil::silly::dry::lol::kiss::D:pinch:
:(:shock::X:side::):P:unsure::woohoo::huh::whistle:;):s
:!::?::idea::arrow:
Please input the anti-spam code that you can read in the image. If it's unreadable, please click on the image. Case insensitive.

3.23 Copyright (C) 2007 Alain Georgette / Copyright (C) 2006 Frantisek Hliva. All rights reserved."

 
Fly.net Artisan maçon