Can I do an optional LEFT JOIN in MySQL?

left-join, mysql, stored-procedures

Solution

Well, you can do something like this:

SELECT a.products
FROM artikelstammdaten a
LEFT JOIN [another_table] ON [your parameterized condition] AND [the real join condition]
(...)

The LEFT JOIN will be done, whatever your condition is, but if it is false, it won't bring back any record.

But you certainly can't emulate the `cfif` in pure SQL :)

Problem

I'm selecting products from a database table. Currently the query is launched from Coldfusion, where I have an "optional LEFT JOIN" like so: ``` SELECT a.products FROM artikelstammdaten a <cfif modul_aktiv("preorder", my_module) IS "true"> LEFT JOIN... </cfif> WHERE ... ``` Question: Is it possible to do an optional LEFT JOIN in MySQL. I'm thinking something like this: ``` SELECT a.products FROM artikelstammdaten a AND ( ((param_preorder = 'false') "(1=2)" ) OR LEFT JOIN ... ) WHERE ... ```

Original source