4

What is difference between these two methods of selecting data from multiple tables. First one does not use JOIN while the second does. Which one is prefered method?

Method 1:

SELECT t1.a, t1.b, t2.c, t2.d, t3.e, t3.f
  FROM table1 t1, table2 t2, table3 t3
 WHERE t1.id = t2.id
   AND t2.id = t3.id
   AND t3.id = x

Method 2:

SELECT t1.a, t1.b, t2.c, t2.d, t3.e, t3.f
FROM `table1` t1
JOIN `table2` t2 ON t1.id = t2.id
JOIN `table3` t3 ON t1.id = t3.id
WHERE t1.id = x
kks
  • 41
  • 1
  • possible duplicate of [Performance of inner join compared to cross join](http://stackoverflow.com/questions/670980/performance-of-inner-join-compared-to-cross-join) – soulmerge Apr 25 '11 at 14:17

3 Answers3

2

For your simple case, they're equivalent. Even though the 'JOIN' keyword is not present in Method #1, it's still doing joins.

However, method #2 offers the flexibility of allowing extra conditions in the JOIN condition that can't be accomplished via WHERE clauses. Such as when you're doing aliased multi-joins against the same table.

select a.id, b.id, c.id
from sometable A
left join othertable as b on a.id=b.a_id and some_condition_in_othertable
left join othertable as c on a.id=c.a_id and other_condition_in_othertable

Putting the two extra conditions in the whereclause would cause the query to return nothing, as both conditions cannot be true at the same time in the where clause, but are possible in the join.

Marc B
  • 356,200
  • 43
  • 426
  • 500
0

The methods are apparently identical in performance, it's just new vs old syntax.

-1

I don't think there is much of a difference. You could use the EXPLAIN statement to check if MySQL does anything differently. For this trivial example I doubt it matters.

Halcyon
  • 57,230
  • 10
  • 89
  • 128