Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MySQL combining records from two tables with join and without

Tags:

sql

join

mysql

What's the difference between these two queries:

SELECT `threads`.`id` AS `threads.id` , `posts`.`id` , `posts`.`content`
FROM `threads`
JOIN `posts` ON `threads`.`id` = `posts`.`thread_id`

And

SELECT `threads`.`id` AS `threads.id` , `posts`.`id` , `posts`.`content`
FROM `threads` , `posts`
WHERE `threads`.`id` = `posts`.`thread_id`

They both return same data.

like image 734
Victor Marchuk Avatar asked Dec 02 '11 12:12

Victor Marchuk


1 Answers

WHERE clause filtering result set which is returned by JOIN, so this is a difference.

As long as you are using INNER JOIN there is no differences neither performance nor execution plan, in case of any OUTER JOIN query would produce different execution plan.

Also pay attention to what is said in the MySql online doc:

Generally, you should use the ON clause for conditions that specify how to join tables, and the WHERE clause to restrict which rows you want in the result set.

like image 116
sll Avatar answered Sep 23 '22 04:09

sll