简体   繁体   English

SQL 查询:如何仅在条件为真时检查 ON 子句中的 Join 条件

[英]SQL Query: How to check for the Join condition in ON clause only if a condition is true

I want to join tables and get the result but I have a condition like, in the ON clause one join condition should be checked only if another condition is true.我想加入表并获得结果,但我有一个条件,例如,在 ON 子句中,只有在另一个条件为真时才应检查一个连接条件。 For Eg:例如:

SELECT a.plan_id, a.plan_name, a.sale_s1, b.sale_s1
from (SELECT plan_id, plan_name, sale_s1, sale_s2
     from RESULT_TABLE
      where plan_id = 1245 and sale_id = (any integer value))a
JOIN (SELECT plan_id, plan_name, sale_s1, sale_s2
        from RESULT_TABLE where plan_id = 1245 and sale_id=(any integer value))b
    ON a.plan_id = b.plan_id AND (2nd CONDITION: SHOULD BE CHECKED only if(sale_id=0))

All the columns are NOT NULLABLE所有列都不是 NULLABLE

Please help me with this, how to achieve the case for that 2nd condition.请帮我解决这个问题,如何实现第二个条件。

If sale_id is NOT NULL:如果 sale_id 不是 NULL:

AND (sale_id <> 0 OR (2nd CONDITION))

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

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