这个问题在这里已经有了答案:
SQL Conditional AND
(4 个回答)
7年前关闭。
我想根据当前行的值确定 JOIN 列。
例如,我的工作表有 4 个日期列:offer_date、accepted_date、start_date、reported_date。
我想根据日期检查汇率。我知道reported_date 永远不会为空,但这是我最后的手段,所以我有一个优先顺序来加入exchange_rate 表。如果这是正确的方法,我不太确定如何使用 CASE 语句执行此操作。
INNER JOIN exchange_rate c1 ON c1.date = {{conditionally pick a column here}}
-- use j.offer_date if not null
-- use j.accepted_date if above is null
-- use j.start_date if above two are null
-- use j.reported_date if above three are null
最佳答案
试试这样的逻辑:
INNER JOIN
exchange_rate c1
ON c1.date = coalesce(j.offer_date, j.accepted_date, j.start_date, j.reported_date)
coalesce()
函数返回列表中的第一个非 NULL 值。
关于SQL 条件 JOIN 列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25255240/