I’m working on a query wherein I have three tables to join, however the third table may or may not have a relevant record.
I currently have:
SELECT table1.val, table2.val, table.3.val
FROM
table1
LEFT JOIN table2
ON table1.val = table2.val
LEFT JOIN table3
ON table.3.val = table2.val
WHERE
table1.name = "name"
AND table3.name = 'certain name'
ORDER BY table1.val ASC
How can I write this so that if 'certain name' doesn’t exist in table3, that portion is omitted from the WHERE and SELECT clauses? I’m actually selecting several more fields but this should adequately demonstrate the problem.
If you mean that result form
table3should only be visible, if thetable3.valis equal to thetable2.valAND that thetable3.nameshould equal'certain name', then this will fit you:After all, this is what
LEFT JOINis for…