2012-04-18 80 views
6

我想對我的用戶列表進行雙重排序。這可能在一個MySQL查詢中嗎?MySQL雙重排序

  1. 排序activity
  2. 排序ID

例如:

1 Jack Active 
2 Jill Active 
5 Jens Active 
3 Harry Inactive 
4 Larry Inactive 
6 Luke Inactive 

回答

8

您可以使用ORDER BY條款根據需要儘可能多的列進行排序。

SELECT id, name, activity 
FROM userList 
ORDER BY Activity, ID 

我建議您閱讀MySQL ORDER BY文檔。 MySQL: ORDER BY Optimization

4
SELECT id, name, activity 
FROM your_table 
ORDER BY activity ASC, id ASC 
1
Select * from table order by activity, ID; 
1
SELECT * 
FROM table_name 
ORDER BY activity, ID; 
0

是,

SELECT (user.name|| ' ' || user.status) AS sorted_list 
FROM STUDENTS 
ORDER BY user.status, user.name 
+0

我想你錯過'FROM user':您可以在ASCDESC對數據進行排序 – 2012-04-18 15:20:24