问题描述
我必须选择满足 condition1 OR condition2
的一行.但是,最好使用 condition1
.如果有两行,第一行满足condition1
(不满足condition2),第二行满足condition2
(不满足condition1
) 然后应该返回第一个.
I have to SELECT one row which meets condition1 OR condition2
. However, condition1
is preferable. If there are two rows, where the first one meets condition1
(and does not meet condition2) and second meets condition2
(and does not meet condition1
) then the first one should be returned.
所以对于 SQL:
SELECT * FROM table WHERE col1 = 1 OR col2 = 5 LIMIT 1
它会首先返回满足条件 col1 = 1
的行吗?还是会以随机顺序返回行?如果它是随机的,那么如何实现我想要的?我必须使用这样的东西?:
Will it return the row that meets condition col1 = 1
first? Or will it return rows in random order? If it will be random then how to achieve what I want? I have to use something like this?:
SELECT * FROM table WHERE col1 = 1 OR col2 = 5
ORDER BY (col1 = 1)::boolean DESC LIMIT 1
推荐答案
WHERE 子句中条件的顺序不会影响结果的优先级.您必须使用 ORDER BY 子句来做到这一点.
The order of the conditions in the WHERE clause does nothing to affect the priority of the results. You have to do that with an ORDER BY clause.
这篇关于SQL - OR 条件的顺序重要吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!