使用 OR 运算符时,查询 return 左边的条件会超过右边吗?

When using an OR operator, will the query return left condition over the right?

我想要 return 一个 单行 但想要一个查询来满足两个条件。

例如

SELECT * FROM user WHERE admin < 10 OR admin > 100 LIMIT 1

当我查询这个时,这个 return 会是它发现的管理值小于 10 的第一行吗?如果没有,我将如何实现?

来自评论:

I want to find the first row that has admin < 10, but if there are no rows that have that value then I want to return the first row that has admin > 100

一个选项使用 union allnot exists:

(select * from users where admin < 10 limit 1)
union all
(
    select * 
    from users 
    where 
        admin > 100 
        and not exists (select 1 from users where admin < 10)
    limit 1
)

另一种解决方案是进行条件排序:

select *
from users 
where admin < 10 and admin > 100
order by case when admin < 10 then 1 else 2 end
limit 1

请注意,如果向其添加 order by 子句,您的查询会更有意义。在没有 order by 的情况下使用 limit 会导致 abritrary 行从满足谓词的行中返回。

您可以使用 order by 执行此操作。我会建议:

SELECT *
FROM user
WHERE admin < 10 OR admin > 100
ORDER BY admin
LIMIT 1