本次讲授如何使用select语句的order by子句,根据需要排序检索出数据。
查看表中所有的记录:select * FROM person ;
MySQL查询结果排序(order by 默认升序排序):
例1:按单个列升序排序,格式:select 列名 from 表名 order by 列名 ASC;
select name FROM person order by name ASC;
例2:按单个列降序排序,格式:select 列名 from 表名 order by 列名 DESC;
select name FROM person order by name DESC;
例3:按多个列升序排序,格式:select 列名 from 表名 order by 列名1 ASC,列名2 ASC;
select * FROM person order by number ASC, name ASC;
例4:按多个列降序排序,格式:select 列名 from 表名 order by 列1 DESC,列2 DESC;
select * FROM person order by number DESC, name DESC;
例5:按多个列先升序后降序排序,格式:select 列名 from 表名 order by 列1 ASC,列2 DESC;
select * FROM person order by number ASC, name DESC;
,