我有以下表格设置
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
有人可以帮忙吗?
答案 0 :(得分:0)
您是否真的尝试自己编写任何 SQL?
SELECT ...
FROM otherTable ot
(INNER) JOIN thisTable tt
ON ot.Acc = tt.Acc AND ot.Currency = tt.Currency
...
答案 1 :(得分:0)
显式匹配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)
或者在您的情况下,使用thistbl
中的NULL货币作为后备
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)