简体   繁体   English

我可以在MySQL中做一个可选的LEFT JOIN吗?

[英]Can I do an optional LEFT JOIN in MySQL?

I'm selecting products from a database table. 我正在从数据库表中选择产品。 Currently the query is launched from Coldfusion, where I have an "optional LEFT JOIN" like so: 当前,该查询是从Coldfusion启动的,其中有一个“可选的LEFT JOIN”,如下所示:

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. 是否可以在MySQL中进行可选的LEFT JOIN。 I'm thinking something like this: 我在想这样的事情:

SELECT a.products
FROM artikelstammdaten a
    AND ( 
        ((param_preorder = 'false') "(1=2)" )
          OR LEFT JOIN ...  
        )
WHERE ...

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. 无论您处于何种状态,都将完成LEFT JOIN,但如果为假,则不会恢复任何记录。

But you certainly can't emulate the cfif in pure SQL :) 但是您当然不能在纯SQL中模拟cfif :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM