Ukebloke Ukebloke - 15 days ago 5
MySQL Question

MySQL WHERE clause with LEFT JOINs

I get a syntax error when use a WHERE clause with the SQL query below. The query works without the where clause ad the where clause works with a simple 'SELECT * FROM' query with no joins. The original query uses PDO and bind parameters but I'm using straight SQL for this example so I can get the syntax right first. Where am I going wrong?

SELECT bk.*, au.author_firstname, au.author_surname, pu.publisher_name
FROM books bk
LEFT JOIN authors au ON bk.author_id = au.author_id
LEFT JOIN publishers pu ON bk.publisher_id = pu.publisher_id
ORDER BY title
WHERE bk.author_id = 13

Answer
SELECT bk.*, au.author_firstname, au.author_surname, pu.publisher_name
FROM books bk
LEFT JOIN authors au ON bk.author_id = au.author_id
LEFT JOIN publishers pu ON bk.publisher_id = pu.publisher_id
WHERE bk.author_id = 13
ORDER BY title

Where comes before ORDER BY.