I have 3 tables which need to be linked in an SQL statement (I’m using PHP – MySQL if it helps). I need to extract all orders where the vendor field from the third table equals ‘3’, as below:
orders - orders_items - items
order_id -> order_id
item_id -> id
vendor = '3'
There are many ways to do this I believe with various WHERE and JOINS but I’m asking for the most efficient methods in comparison to my method below:
SELECT
orders.order_id
FROM
items, orders
INNER JOIN
orders_items
ON
orders.order_id = orders_items.order_id
WHERE
orders_items.item_id = items.id
AND
items.vendor = '3'
GROUP BY
orders.order_id
Using
,notation is not universally considered bad practice, but I think it’s quite a minority now that agree with it. Even Oracle (whose users seems to be the most vocal supporters of that syntax) recommend to not use it.But I don’t know anyone who would support mixing
,and ANSI-92’sJOINsyntax. It’s just asking for trouble.The SQL Optimiser doesn’t execute that exactly as you specified it. SQL is just a expression from which the SQL Optimiser derives a plan to give a result that fits. By writing it as above the optimiser will find what it sees as the best order to filter, join, sort, etc, and which are the best indexes, etc to use to do those things.
EDIT
I’ve noticed people supporting
DISTINCToverGROUP BY.While
DISTINCTis slightly shorter, it is not any quicker, and does place restrictions on you. You can’t later addCOUNT(*)for example, but withGROUP BYyou can.In short,
GROUP BYcan do anythingDISTINCTcan, but that’s not true the other way around. I only useDISTINCTin very trivial pieces of code so I can get a shole query on one line. Even then I often later regret it a little as the code develops and I need to rever toGROUP BY.