我有下表设置
Acc Currency Alias
1 NULL A
1 USD B
1 EUR C
我想通过将输入作为Acc
. 和连接到另一个表Currency
。Acc
它应该按如下方式工作 -
If acc = 1 and currency is USD then B, if EUR then C, if null then A
有人可以帮忙吗?
我有下表设置
Acc Currency Alias
1 NULL A
1 USD B
1 EUR C
我想通过将输入作为Acc
. 和连接到另一个表Currency
。Acc
它应该按如下方式工作 -
If acc = 1 and currency is USD then B, if EUR then C, if null then A
有人可以帮忙吗?
你真的尝试过自己编写任何SQL 吗?
SELECT ...
FROM otherTable ot
(INNER) JOIN thisTable tt
ON ot.Acc = tt.Acc AND ot.Currency = tt.Currency
...
显式匹配 NULL
SELECT *
FROM othertbl o
JOIN thistbl t on o.acc = t.acc
and (o.currency = t.currency or o.currency is null AND t.currency is null)
或者在您的情况下,使用 NULL 货币thistbl
作为后备
SELECT ...
FROM othertbl o
JOIN thistbl t on o.acc = t.acc and o.currency = t.currency
UNION ALL
SELECT ...
FROM othertbl o
JOIN thistbl t on o.acc = t.acc and t.currency IS NULL
WHERE NOT EXISTS (select *
from thistbl t2
where o.acc = t2.acc and o.currency = t2.currency)