MySQL: Quick breakdown of the types of joins [duplicate]
I would like a quick breakdown of the types of MySQL joins. I know of these, the rest I am not sure what they mean.
- comma separated (what exactly is this short for?):
SELECT * FROM a, b WHERE b.id = a.beeId AND ...
- show information from a, even if there are no matches in b:
SELECT * FROM a LEFT OUTER JOIN b ON b.id = a.beeId WHERE ...
I have seen other joins, but want to know what makes them different, what is INNER
/OUTER
, does adding LEFT
change things.
I already know how joins work, I just want to know if there are other types of joins, or if they are just different ways to get the same result.
Solution 1:
Based on your comment, simple definitions of each is best found at W3Schools The first line of each type gives a brief explanation of the join type
- JOIN: Return rows when there is at least one match in both tables
- LEFT JOIN: Return all rows from the left table, even if there are no matches in the right table
- RIGHT JOIN: Return all rows from the right table, even if there are no matches in the left table
- FULL JOIN: Return rows when there is a match in one of the tables
END EDIT
In a nutshell, the comma separated example you gave of
SELECT * FROM a, b WHERE b.id = a.beeId AND ...
is selecting every record from tables a and b with the commas separating the tables, this can be used also in columns like
SELECT a.beeName,b.* FROM a, b WHERE b.id = a.beeId AND ...
It is then getting the instructed information in the row where the b.id column and a.beeId column have a match in your example. So in your example it will get all information from tables a and b where the b.id equals a.beeId. In my example it will get all of the information from the b table and only information from the a.beeName column when the b.id equals the a.beeId. Note that there is an AND clause also, this will help to refine your results.
For some simple tutorials and explanations on mySQL joins and left joins have a look at Tizag's mySQL tutorials. You can also check out Keith J. Brown's website for more information on joins that is quite good also.
I hope this helps you
Solution 2:
I have 2 tables like this:
> SELECT * FROM table_a;
+------+------+
| id | name |
+------+------+
| 1 | row1 |
| 2 | row2 |
+------+------+
> SELECT * FROM table_b;
+------+------+------+
| id | name | aid |
+------+------+------+
| 3 | row3 | 1 |
| 4 | row4 | 1 |
| 5 | row5 | NULL |
+------+------+------+
INNER JOIN cares about both tables
INNER JOIN cares about both tables, so you only get a row if both tables have one. If there is more than one matching pair, you get multiple rows.
> SELECT * FROM table_a a INNER JOIN table_b b ON a.id=b.aid;
+------+------+------+------+------+
| id | name | id | name | aid |
+------+------+------+------+------+
| 1 | row1 | 3 | row3 | 1 |
| 1 | row1 | 4 | row4 | 1 |
+------+------+------+------+------+
It makes no difference to INNER JOIN if you reverse the order, because it cares about both tables:
> SELECT * FROM table_b b INNER JOIN table_a a ON a.id=b.aid;
+------+------+------+------+------+
| id | name | aid | id | name |
+------+------+------+------+------+
| 3 | row3 | 1 | 1 | row1 |
| 4 | row4 | 1 | 1 | row1 |
+------+------+------+------+------+
You get the same rows, but the columns are in a different order because we mentioned the tables in a different order.
LEFT JOIN only cares about the first table
LEFT JOIN cares about the first table you give it, and doesn't care much about the second, so you always get the rows from the first table, even if there is no corresponding row in the second:
> SELECT * FROM table_a a LEFT JOIN table_b b ON a.id=b.aid;
+------+------+------+------+------+
| id | name | id | name | aid |
+------+------+------+------+------+
| 1 | row1 | 3 | row3 | 1 |
| 1 | row1 | 4 | row4 | 1 |
| 2 | row2 | NULL | NULL | NULL |
+------+------+------+------+------+
Above you can see all rows of table_a even though some of them do not match with anything in table b, but not all rows of table_b - only ones that match something in table_a.
If we reverse the order of the tables, LEFT JOIN behaves differently:
> SELECT * FROM table_b b LEFT JOIN table_a a ON a.id=b.aid;
+------+------+------+------+------+
| id | name | aid | id | name |
+------+------+------+------+------+
| 3 | row3 | 1 | 1 | row1 |
| 4 | row4 | 1 | 1 | row1 |
| 5 | row5 | NULL | NULL | NULL |
+------+------+------+------+------+
Now we get all rows of table_b, but only matching rows of table_a.
RIGHT JOIN only cares about the second table
a RIGHT JOIN b
gets you exactly the same rows as b LEFT JOIN a
. The only difference is the default order of the columns.
> SELECT * FROM table_a a RIGHT JOIN table_b b ON a.id=b.aid;
+------+------+------+------+------+
| id | name | id | name | aid |
+------+------+------+------+------+
| 1 | row1 | 3 | row3 | 1 |
| 1 | row1 | 4 | row4 | 1 |
| NULL | NULL | 5 | row5 | NULL |
+------+------+------+------+------+
This is the same rows as table_b LEFT JOIN table_a
, which we saw in the LEFT JOIN section.
Similarly:
> SELECT * FROM table_b b RIGHT JOIN table_a a ON a.id=b.aid;
+------+------+------+------+------+
| id | name | aid | id | name |
+------+------+------+------+------+
| 3 | row3 | 1 | 1 | row1 |
| 4 | row4 | 1 | 1 | row1 |
| NULL | NULL | NULL | 2 | row2 |
+------+------+------+------+------+
Is the same rows as table_a LEFT JOIN table_b
.
No join at all gives you copies of everything
If you write your tables with no JOIN clause at all, just separated by commas, you get every row of the first table written next to every row of the second table, in every possible combination:
> SELECT * FROM table_b b, table_a;
+------+------+------+------+------+
| id | name | aid | id | name |
+------+------+------+------+------+
| 3 | row3 | 1 | 1 | row1 |
| 3 | row3 | 1 | 2 | row2 |
| 4 | row4 | 1 | 1 | row1 |
| 4 | row4 | 1 | 2 | row2 |
| 5 | row5 | NULL | 1 | row1 |
| 5 | row5 | NULL | 2 | row2 |
+------+------+------+------+------+
(This is from my blog post Examples of SQL join types)
Solution 3:
Full Outer join don't exist in mysql , you might need to use a combination of left and right join.