我正在进行级联SELECT:
SELECT * FROM x WHERE a = 1 AND b = 2 AND c = 3
=> If nothing found, try: SELECT * FROM x WHERE a = 1 AND b = 2 AND c = 0
=> If nothing found, try: SELECT * FROM x WHERE a = 1 AND b = 0 AND c = 0
其中至少有一个有结果,但所有情况都可能有结果。我想合并这个逻辑并仅返回一个案例,即具有最多给定(!= 0)条件的案例。
谢谢!
答案 0 :(得分:1)
我可以使用UNION查询回答并且不是EXISTS:
SELECT * FROM x
WHERE
(a, b, c) = (
SELECT a, b, c
FROM x WHERE (a, b, c) IN ( (1,2,3), (1,2,0), (1,0,0) )
ORDER BY (b=0) + (c=0)
LIMIT 1
)
但更聪明的解决方案是这样的:
{{1}}
我不确定这最后一个会有更好的表现。我会尝试哪一个更好。