A query to grab the list of phone numbers for clients who ordered in the last two weeks might be written in a couple of ways. Here are two:
SELECT * FROM clients, orders, phoneNumbers WHERE clients.id = orders.clientId AND clients.id = phoneNumbers.clientId AND orderPlaced >= NOW() - INTERVAL 2 WEEK;
SELECT * FROM clients INNER JOIN orders ON clients.id = orders.clientId INNER JOIN phoneNumbers ON clients.id = phoneNumbers.clientId WHERE orderPlaced >= NOW() - INTERVAL 2 WEEK;
Does it make a difference? Not much as written. But you should use the second form. Why?
INNER JOIN phoneNumbers
to LEFT JOIN phoneNumbers
. Try that with the first version, and MySQL version 5.0.12+ will issue a syntax error because of the change in precedence between the comma operator and the JOIN keyword. The solution is to rearrange the FROM clause or add parentheses to override the precedence, and that quickly becomes frustrating. The initial version of this article was copied, with permission, from http://hashmysql.org/wiki/Comma_vs_JOIN on 2012-10-05.
© 2019 MariaDB
Licensed under the Creative Commons Attribution 3.0 Unported License and the GNU Free Documentation License.
https://mariadb.com/kb/en/comma-vs-join/