我希望在同一列上说明三个if / case条件
SELECT u.username, u.fullname, u.email,
(IF u.status = "done" AND ui.itemtotal != "NULL" THEN 0
IF u.status = "done" AND ui.itemtotal = "NULL" THEN 1
IF u.status != "done" AND ui.itemtotal != "NULL" THEN 2) AS "Item.Status"
FROM
list_users u
JOIN list_items ui on ui.userid = u.id
答案 0 :(得分:0)
使用CASE
声明:
SELECT u.username,
u.fullname,
u.email,
case when u.status = 'done' AND ui.itemtotal != 'NULL' then 0
when u.status = 'done' AND ui.itemtotal = 'NULL' then 1
when u.status != 'submitted' AND ui.itemtotal != 'NULL' then 2
end as "Item.Status"
FROM
list_users u
JOIN list_items ui on ui.userid = u.id
检查NULL
值时也要小心。我假设您正在检查NULL
的字符串值。如果没有,请使用IS NULL
或IS NOT NULL
:
case when u.status = 'done' AND ui.itemtotal IS NOT NULL then 0
when u.status = 'done' AND ui.itemtotal IS NULL then 1
when u.status != 'submitted' AND ui.itemtotal IS NOT NULL then 2