假设我在sqlite3中有此表。
name, currency, price
AA, SGD, 1
BB, USD, 2
CC, EUR, 3
我需要以这种方式处理price
。
if currency == "SGD", price = price*2
if currency == "USD", price = price*3
if currency == "EUR", price = price*4
处理后的输出表如下所示:
name, currency, price
AA, SGD, 2
BB, USD, 6
CC, EUR, 12
是否可以为sqlite3数据库编写某种函数来处理sql中的price
列?
答案 0 :(得分:2)
使用CASE
表达式:
SELECT
name,
currency,
CASE WHEN currency = 'SGD' THEN 2*price
WHEN currency = 'USD' THEN 3*price
WHEN currency = 'EUR' THEN 4*price
ELSE price END AS price
FROM yourTable;