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 mytable where id = 42) union all (select * from mytable where id != 42 order by id)
除了ansi查询变体之外,mysql对我来说也是非常有趣的
ansi
mysql
ANSI 和供应商 SQL 解决方案之间没有区别
select * from mytable order by CASE WHEN id = 42 THEN 0 ELSE 1 END, id
注意:没有 ORDER BY 子句的表或 SELECT没有保证或隐含的顺序
假设 id 始终为正,请尝试:
select * from mytable order by CASE WHEN id = 42 THEN 0 ELSE id END