JaT*_*Dan 5 mysql sql select where
基本上我想这样做:
SELECT * FROM `table` WHERE x = 'hello' OR x = 'bye' LIMIT 1';
Run Code Online (Sandbox Code Playgroud)
我希望它返回1个值,但要优先考虑1st where子句的结果.因此,如果存在列x的值为"hello"的行,则它不会返回"bye"值的结果.如果"hello"值不存在,它将返回"bye"值的结果.
即使看起来相当微不足道,也无法想出办法.有任何想法吗?
一种方法是使用order by:
SELECT *
FROM `table`
WHERE x = 'hello' OR x = 'bye'
order by (case when x = 'hello' then 1 else 2 end)
LIMIT 1'
Run Code Online (Sandbox Code Playgroud)
"1"和"2"只是用于对行进行优先级排序的任意数字.'hello'的值变为"1",因此它们在其他值之前排序.
| 归档时间: |
|
| 查看次数: |
1952 次 |
| 最近记录: |