如果我的值存在,我想返回一行,如果不存在则返回另一行。
我有以下结果:
ID | Row_To_Check
1 | 1
2 | 2
因此,如果2
列中存在Row_To_Check
,我想将其返回(ID
2
),否则我想返回ID
{ {1}}。
有人可以帮忙吗?这让我很难过。
答案 0 :(得分:1)
试试这个:
SELECT COALESCE(Row_To_Check, x) AS Row_To_Check
FROM (SELECT 1 AS x UNION ALL SELECT 2) AS t
LEFT JOIN mytable ON Row_To_Check = 2
WHERE (x = 1 AND Row_To_Check IS NULL) OR (x = Row_To_Check)
答案 1 :(得分:0)
我不知道是否有更好的解决方案,但这应该可以解决问题
SELECT id FROM (
SELECT id, 1 as priority FROM table WHERE Row_To_Check = 2
UNION
SELECT id, 2 as priority FROM table WHERE id = 1
) as my_table
ORDER BY priority ASC
LIMIT 1