Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
基本上我想这样做:
SELECT * FROM `table` WHERE x = 'hello' OR x = 'bye' LIMIT 1';
我希望它返回 1 个值,但要优先考虑第一个 where 子句的结果。因此,如果存在 x 列的值为“hello”的行,它将不会返回“bye”值的结果。如果“hello”值不存在,它将返回“bye”值的结果。
想不出办法来做到这一点,即使它看起来相当微不足道。有任何想法吗?
一种方法是使用order by:
order by
SELECT * FROM `table` WHERE x = 'hello' OR x = 'bye' order by (case when x = 'hello' then 1 else 2 end) LIMIT 1'
“1”和“2”只是用于确定行优先级的任意数字。'hello' 的值得到“1”,因此它们在其他值之前排序。