如何按照多列对 MySQL 行进行排序?
我们先创建一个表 −
mysql> create table DemoTable -> ( -> FirstName varchar(20), -> LastName varchar(20) -> ); Query OK, 0 rows affected (1.44 sec)
使用 insert 命令在表中插入一些记录 −
mysql> insert into DemoTable values('John','Smith'); Query OK, 1 row affected (0.35 sec) mysql> insert into DemoTable values('Chris','Brown'); Query OK, 1 row affected (0.37 sec) mysql> insert into DemoTable values('Carol','Taylor'); Query OK, 1 row affected (0.15 sec) mysql> insert into DemoTable values('David','Miller'); Query OK, 1 row affected (0.24 sec)
使用 select 语句从表中显示所有记录 −
mysql> select *from DemoTable;
输出
将产生以下输出 −
+-----------+----------+ | FirstName | LastName | +-----------+----------+ | John | Smith | | Chris | Brown | | Carol | Taylor | | David | Miller | +-----------+----------+ 4 rows in set (0.00 sec)
以下是按照多列对 MySQL 行进行排序的查询 −
mysql> select *from DemoTable order by FirstName,LastName
输出
将产生以下输出 −
+-----------+----------+ | FirstName | LastName | +-----------+----------+ | Carol | Taylor | | Chris | Brown | | David | Miller | | John | Smith | +-----------+----------+ 4 rows in set (0.24 sec)
广告