If I have
SELECT * FROM Table1 t1 LEFT JOIN Table2 t2 ON t1.id = t2.id WHERE t1.user='bob';
Does the WHERE
clause run after the two tables are JOINED
?
How do I make it so it runs prior to the JOIN?
The where clause will be executed before the join so that it doesn't join unnecessary records. So your code is fine the way it is.
The rows selected by a query are filtered first by the FROM clause join conditions, then the WHERE clause search conditions, and then the HAVING clause search conditions.
Inner Join syntax basically compares rows of Table1 with Table2 to check if anything matches based on the condition provided in the ON clause. When the Join condition is met, it returns matched rows in both tables with the selected columns in the SELECT clause.
You join two tables by creating a relationship in the WHERE clause between at least one column from one table and at least one column from another. The join creates a temporary composite table where each pair of rows (one from each table) that satisfies the join condition is linked to form a single row.
The where
clause will be executed before the join
so that it doesn't join unnecessary records. So your code is fine the way it is.
Change the WHERE
to another JOIN
condition
LEFT JOIN Table2 t2 on t1.id = t2.id AND t1.user='bob'
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With