按 MYSQL 中的特定列排序,有额外条件

Order by a specific column in MYSQL, with extra condition

有个table

Teacher
--------
id
name
country_id

我可以简单地用 order by country_id asc 给老师点单,但是,如何提供一个特定的 id 然后点单呢?

例如,我希望 country_id 3 的结果排在第一位,这意味着如果有国家 ID 1 或 2,它们应该在国家 ID 3

之后
e.g. 

teacherA 3
teacherB 3
teacherC 1
teacherD 4
etc..

非常感谢

试试这个:

ORDER BY IF(country_id = 3, 0, 1),
         country_id 

我建议您使用 Field()。 这类似于按字段排序 (country_id, 3, 2),这将为您提供国家 ID,首先是 3,然后是国家 ID 2,然后将对其他所有内容进行排序。