对多列进行排序
使用ORDER BY子句也可以根据两列或多列的结果进行排序,只要用逗号分隔开不同的排序关键字就可以了。如下面的语句:
ORDER BY col1 DESC,col2,……,colm |
其实际排序结果是根据ORDER BY子句后面列名的顺序确定优先级的。即查询结果首先以列col1的降序顺序进行排序,而只有当列col1出现相同的信息时,这些相同的信息再按col2列的升序进行排序,依此类推。
CREATE TABLE tb (a varchar(50), b varchar(50), c varchar(50) ) INSERT INTO tb select '1','2','9' union all select '1','3','3' union all select '2','5','4' union all select '2','5','8' union all select '2','6','7' union all select '2','5','6' union all select '2','6','1' union all select '3','4','2' union all select '4','4','4' SELECT * FROM tb SELECT * FROM tb ORDER BY A ASC SELECT * FROM tb ORDER BY A ASC,B DESC SELECT * FROM tb ORDER BY A ASC,B DESC,C DESC