The other answers posted here are the correct solutions.
I'll answer the "Why do I get 1064" for you.
The syntax you've written is invalid. You cannot "conditionally" join to a table like this.
What you can do is LEFT JOIN
- which as opposed to [INNER] JOIN
means "return NULL
for all fields on this table if no record matches the join condition".
There's lots on the Internet about LEFT
vs INNER
joins, and MySQL's website documents the COALESCE
function very well.
Have a play and a read, you'll figure it out why the other solutions work.
Upon reflection, I felt this could benefit from some additional explanation.
See this SQLFiddle: http://sqlfiddle.com/#!9/043b7/6