3

我有这样的桌子

id    name
1     John
2     Mike
3     Zed
4     Teacher
5     Aaron
...........

如何进行mysql查询以按名称按ASC顺序获取行,但将“Teacher”放在顶部或以“Teacher”开头的东西?

4

3 回答 3

11
select * from your_table
order by case when name = 'Teacher' then 1 else 2 end,
         name 

对于 MySQL,以下是有效的,因为它返回0forfalse1fortrue

select * from your_table
order by name <> 'Teacher',
         name 
于 2013-10-08T08:22:15.533 回答
0

使用FIELD函数

SELECT * FROM table ORDER BY FIELD(`name`,'Teacher') ASC
于 2013-10-08T08:22:43.670 回答
0
select * from table order by case when name like "Teacher%" then 1 else 2 end,name 

以老师开头的名字将按字母顺序排列在顶部,然后是其余的。例子

Teacher
Teacher Aaron
Teacher Zed
Aaron
Zed
于 2013-10-08T08:29:52.347 回答