通过两列订购MySQL表?
在以下语法的帮助下,将MySQL表按两列排序-
order by yourColumnName1 DESC,yourColumnName2 DESC;
让我们首先为我们的示例创建一个表-
mysql> create table OrderByDemo -> ( -> StudentId int, -> StudentName varchar(100), -> StudentAge int -> );
借助insert命令将记录插入表中。查询如下-
mysql> insert into OrderByDemo values(1,'John',23); mysql> insert into OrderByDemo values(3,'Johnson',24); mysql> insert into OrderByDemo values(4,'Carol',26); mysql> insert into OrderByDemo values(2,'David',20);
现在,将上述语法应用于MySQL表中的两列。查询如下-
mysql> select *from OrderByDemo order by StudentId ASC, StudentAge ASC;
以下是按升序对两列进行排序的输出-
+-----------+-------------+------------+ | StudentId | StudentName | StudentAge | +-----------+-------------+------------+ | 1 | John | 23 | | 2 | David | 20 | | 3 | Johnson | 24 | | 4 | Carol | 26 | +-----------+-------------+------------+ 4 rows in set (0.00 sec)
或者,您可以在DESC命令的帮助下按降序进行操作。查询如下-
mysql> select *from OrderByDemo order by StudentId DESC,StudentAge DESC;
以下是输出-
+-----------+-------------+------------+ | StudentId | StudentName | StudentAge | +-----------+-------------+------------+ | 4 | Carol | 26 | | 3 | Johnson | 24 | | 2 | David | 20 | | 1 | John | 23 | +-----------+-------------+------------+ 4 rows in set (0.00 sec)
注意-主要排序首先起作用。
热门推荐
10 新人红包祝福语简短精辟
11 幼师给毕业祝福语简短
12 生日高考祝福语妹妹简短
13 下飞机祝福语简短英文
14 元宵发客户祝福语简短
15 爸妈金婚文案祝福语简短
16 新郎朋友红包祝福语简短
17 虎年五一祝福语大全简短
18 出阁宴祝福语姐姐简短