简体   繁体   中英

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. 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

Please help me with this, how to achieve the case for that 2nd condition.

If sale_id is NOT NULL:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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