簡體   English   中英

SQL 查詢:如何僅在條件為真時檢查 ON 子句中的 Join 條件

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

我想加入表並獲得結果,但我有一個條件,例如,在 ON 子句中,只有在另一個條件為真時才應檢查一個連接條件。 例如:

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

所有列都不是 NULLABLE

請幫我解決這個問題,如何實現第二個條件。

如果 sale_id 不是 NULL:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM