我有这样的桌子
id name
1 John
2 Mike
3 Zed
4 Teacher
5 Aaron
...........
如何进行mysql查询以按名称按ASC顺序获取行,但将“Teacher”放在顶部或以“Teacher”开头的东西?
select * from your_table
order by case when name = 'Teacher' then 1 else 2 end,
name
对于 MySQL,以下是有效的,因为它返回0
forfalse
和1
fortrue
select * from your_table
order by name <> 'Teacher',
name
使用FIELD函数
SELECT * FROM table ORDER BY FIELD(`name`,'Teacher') ASC
select * from table order by case when name like "Teacher%" then 1 else 2 end,name
以老师开头的名字将按字母顺序排列在顶部,然后是其余的。例子
Teacher
Teacher Aaron
Teacher Zed
Aaron
Zed