Reputation: 261
I have a table of users which contain some information of users.
id | name | country
------------------------------
1 | user1 | India
2 | user2 | China
3 | user3 | United State
4 | user4 | India
5 | user5 | Shri Lanka
6 | user6 | China
7 | user7 | India
I want to make a search for users and write a Mysql query.The users list i want like order by id desc but if the users of country China should come at last.
select * from users order by id desc
This query gives me the result in descending order but i don't know what i write about country.
My Output should like
id | name | country
------------------------------
7 | user7 | India
5 | user5 | Shri Lanka
4 | user4 | India
3 | user3 | United State
1 | user1 | India
6 | user6 | China
2 | user2 | China
Upvotes: 2
Views: 832
Reputation: 163
Try the following
SELECT * FROM users ORDER BY country = 'China',id DESC
The conditional ordering can satisfy your issue .
Upvotes: 2
Reputation: 40471
You can use conditional ordering :
ORDER BY t.country = 'China', // 1 if true, 0 if false, ordered ASC
t.id DESC
MySQL accepts boolean expressions , so t.country = 'China'
will be 1 if true, and 0 if false.
Upvotes: 3