Fer*_*cal 4 sql database conditional
也许你可以帮我一个SQL查询:
我在辅助表中有一个转换值和以下结构:
ID PRICE_BRL PRICE_USD
-- --------- ---------
1 10 5
2 12 NULL
3 NULL 3
4 14 NULL
5 NULL 4
6 NULL NULL
Run Code Online (Sandbox Code Playgroud)
我需要一个结果集就像优先考虑第一列一样,如果为NULL,则给出第二列值乘以存储在辅助表中的转换值.像伪代码一样:
SELECT
id,
(
IF (price_brl != null)
price_brl
ELSE
price_usd * tbl_2.value
) as final_price
FROM tbl_1
Run Code Online (Sandbox Code Playgroud)
我认为使用Joins一定很简单,但我无法理解!
提前致谢.
伪码也是:
select id, coalesce(price_brl, price_usd * tbl_2.value)
from tbl_1
inner join tbl2
Run Code Online (Sandbox Code Playgroud)