MySQL 查询常用操作——排序 order by
Posted 酸菜鱼土豆大侠
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了MySQL 查询常用操作——排序 order by相关的知识,希望对你有一定的参考价值。
mysql中常用的查询操作,首先是能直接从表中直接取出数据,接着能对查询结果做一些简单的处理,比如去重等,然后是根据条件查询数据,包括精准查询、模糊查询以及按照数据的某个范围或者指定多个指标进行查询,值得注意的是,MySQL中的一些查询会忽略空值。最后是将查询结果进行排序,主要包括升序和降序以及多个字段的先后排序处理。
第三部分主要介绍order by 语句——对查询结果进行排序。
一、具体方法
对查询结果排序
1、表达式后面可选地跟着 asc 或 desc 来指示排序方向。如果没有指定方向,默认为 asc。 2、升序时先对空值排序,降序时最后对空值排序。
问题 | 类型 | 方法 |
---|---|---|
按照用户年龄进行升序排列 | 升序 | order by a |
先按照age升序,再按照gpa升序 | 多列排序 | order by a,b |
先按照age降序,再按照gpa降序 | 多列降序 | order by a desc, b desc |
扩展:
高级用法前往:MySQL union 和 order by 一起使用需要注意的问题
二、实例
已知有用户信息表user_profile(device_id 设备ID, gender 性别, age 年龄, university 学校, gpa)
id | device_id | gender | age | university | gpa |
---|---|---|---|---|---|
1 | 2138 | male | 21 | 北京大学 | 3.4 |
2 | 3214 | male | 复旦大学 | 4.0 | |
3 | 6543 | female | 20 | 北京大学 | 3.2 |
4 | 2315 | female | 23 | 浙江大学 | 3.6 |
5 | 5432 | male | 25 | 山东大学 | 3.8 |
6 | 2131 | male | 28 | 北京师范大学 | 3.3 |
题目1:取出用户的device_id、age、university信息,并按照用户年龄进行升序排列
【分类】:升序
【知识点】:order by a
求解代码
select
device_id,
age,
university
from user_profile
order by age;
结果
device_id | age | university |
---|---|---|
3214 | None | 复旦大学 |
6543 | 20 | 北京大学 |
2138 | 21 | 北京大学 |
2315 | 23 | 浙江大学 |
5432 | 25 | 山东大学 |
2131 | 28 | 北京师范大学 |
题目2:取出用户信息表中的device_id,gpa,age数据,并先按照gpa升序排序,再按照age升序排序输出
【分类】:多列升序
【知识点】:order by a ,b
求解代码
select
device_id,
age,
gpa
from user_profile
order by age,gpa;
结果
device_id | age | gpa |
---|---|---|
3214 | None | 4.0 |
6543 | 20 | 3.2 |
2138 | 21 | 3.4 |
2315 | 23 | 3.6 |
5432 | 25 | 3.8 |
2131 | 28 | 3.3 |
题目3:取出用户信息表中的device_id,gpa,age数据,并先按照gpa、年龄降序排序输出
【分类】:多列降序
【知识点】: order by a desc, b desc
求解代码
select
device_id,
age,
gpa
from user_profile
order by age desc,gpa desc;
结果
device_id | age | gpa |
---|---|---|
2131 | 28 | 3.3 |
5432 | 25 | 3.8 |
2315 | 23 | 3.6 |
2138 | 21 | 3.4 |
6543 | 20 | 3.2 |
3214 | None | 4.0 |
创建 user_profile 表的代码
-- ----------------------------
-- Table structure for user_profile
-- ----------------------------
DROP TABLE IF EXISTS `user_profile`;
CREATE TABLE `user_profile` (
`id` int(11) NOT NULL,
`device_id` int(11) NOT NULL,
`gender` varchar(14) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,
`age` int(11) NULL DEFAULT NULL,
`university` varchar(32) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,
`gpa` float NULL DEFAULT NULL
) ENGINE = InnoDB CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Dynamic;
-- ----------------------------
-- Records of user_profile
-- ----------------------------
INSERT INTO `user_profile` VALUES (1, 2138, 'male', 21, '北京大学', 3.4);
INSERT INTO `user_profile` VALUES (2, 3214, 'male', NULL, '复旦大学', 4);
INSERT INTO `user_profile` VALUES (3, 6543, 'female', 20, '北京大学', 3.2);
INSERT INTO `user_profile` VALUES (4, 2315, 'female', 23, '浙江大学', 3.6);
INSERT INTO `user_profile` VALUES (5, 5432, 'male', 25, '山东大学', 3.8);
INSERT INTO `user_profile` VALUES (6, 2131, 'male', 28, '北京师范大学', 3.3);
SET FOREIGN_KEY_CHECKS = 1;
mysql 数据操作 单表查询 查询排序: order by
如果不指定排序 默认是按照id字段 从小到大排序的 升序
mysql> select * from employee; +----+------------+--------+-----+------------+-----------+--------------+------------+--------+-----------+ | id | name | sex | age | hire_date | post | post_comment | salary | office | depart_id | +----+------------+--------+-----+------------+-----------+--------------+------------+--------+-----------+ | 1 | alex | male | 78 | 2015-03-02 | teacher | NULL | 1000000.31 | 401 | 1 | | 2 | yuanhao | male | 73 | 2014-07-01 | teacher | NULL | 3500.00 | 401 | 1 | | 3 | liwenzhou | male | 28 | 2012-11-01 | teacher | NULL | 2100.00 | 401 | 1 | | 4 | jingliyang | female | 18 | 2011-02-11 | teacher | NULL | 9000.00 | 401 | 1 | | 5 | jinxin | male | 18 | 1900-03-01 | teacher | NULL | 30000.00 | 401 | 1 | | 6 | 成龙 | male | 48 | 2010-11-11 | teacher | NULL | 10000.00 | 401 | 1 | | 7 | 歪歪 | female | 48 | 2015-03-11 | sale | NULL | 3000.13 | 402 | 2 | | 8 | 丫丫 | female | 38 | 2010-11-01 | sale | NULL | 2000.35 | 402 | 2 | | 9 | 丁丁 | female | 18 | 2011-03-12 | sale | NULL | 1000.37 | 402 | 2 | | 10 | 星星 | female | 18 | 2016-05-13 | sale | NULL | 3000.29 | 402 | 2 | | 11 | 格格 | female | 28 | 2017-01-27 | sale | NULL | 4000.33 | 402 | 2 | | 12 | 张野 | male | 28 | 2016-03-11 | operation | NULL | 10000.13 | 403 | 3 | | 13 | 程咬金 | male | 18 | 1997-03-12 | operation | NULL | 20000.00 | 403 | 3 | | 14 | 程咬银 | female | 18 | 2013-03-11 | operation | NULL | 19000.00 | 403 | 3 | | 15 | 程咬铜 | male | 18 | 2015-04-11 | operation | NULL | 18000.00 | 403 | 3 | | 16 | 程咬铁 | female | 18 | 2014-05-12 | operation | NULL | 17000.00 | 403 | 3 | +----+------------+--------+-----+------------+-----------+--------------+------------+--------+-----------+ 16 rows in set (0.10 sec)
order by 不指定 asc 默认就是升序
需求 按照年龄从小到大 排序 升序
asc 是升序
mysql> select * from employee order by age asc; +----+------------+--------+-----+------------+-----------+--------------+------------+--------+-----------+ | id | name | sex | age | hire_date | post | post_comment | salary | office | depart_id | +----+------------+--------+-----+------------+-----------+--------------+------------+--------+-----------+ | 16 | 程咬铁 | female | 18 | 2014-05-12 | operation | NULL | 17000.00 | 403 | 3 | | 15 | 程咬铜 | male | 18 | 2015-04-11 | operation | NULL | 18000.00 | 403 | 3 | | 14 | 程咬银 | female | 18 | 2013-03-11 | operation | NULL | 19000.00 | 403 | 3 | | 13 | 程咬金 | male | 18 | 1997-03-12 | operation | NULL | 20000.00 | 403 | 3 | | 10 | 星星 | female | 18 | 2016-05-13 | sale | NULL | 3000.29 | 402 | 2 | | 9 | 丁丁 | female | 18 | 2011-03-12 | sale | NULL | 1000.37 | 402 | 2 | | 5 | jinxin | male | 18 | 1900-03-01 | teacher | NULL | 30000.00 | 401 | 1 | | 4 | jingliyang | female | 18 | 2011-02-11 | teacher | NULL | 9000.00 | 401 | 1 | | 12 | 张野 | male | 28 | 2016-03-11 | operation | NULL | 10000.13 | 403 | 3 | | 3 | liwenzhou | male | 28 | 2012-11-01 | teacher | NULL | 2100.00 | 401 | 1 | | 11 | 格格 | female | 28 | 2017-01-27 | sale | NULL | 4000.33 | 402 | 2 | | 8 | 丫丫 | female | 38 | 2010-11-01 | sale | NULL | 2000.35 | 402 | 2 | | 7 | 歪歪 | female | 48 | 2015-03-11 | sale | NULL | 3000.13 | 402 | 2 | | 6 | 成龙 | male | 48 | 2010-11-11 | teacher | NULL | 10000.00 | 401 | 1 | | 2 | yuanhao | male | 73 | 2014-07-01 | teacher | NULL | 3500.00 | 401 | 1 | | 1 | alex | male | 78 | 2015-03-02 | teacher | NULL | 1000000.31 | 401 | 1 | +----+------------+--------+-----+------------+-----------+--------------+------------+--------+-----------+ 16 rows in set (0.00 sec)
不加asc
mysql> select * from employee order by age ; +----+------------+--------+-----+------------+-----------+--------------+------------+--------+-----------+ | id | name | sex | age | hire_date | post | post_comment | salary | office | depart_id | +----+------------+--------+-----+------------+-----------+--------------+------------+--------+-----------+ | 16 | 程咬铁 | female | 18 | 2014-05-12 | operation | NULL | 17000.00 | 403 | 3 | | 15 | 程咬铜 | male | 18 | 2015-04-11 | operation | NULL | 18000.00 | 403 | 3 | | 14 | 程咬银 | female | 18 | 2013-03-11 | operation | NULL | 19000.00 | 403 | 3 | | 13 | 程咬金 | male | 18 | 1997-03-12 | operation | NULL | 20000.00 | 403 | 3 | | 10 | 星星 | female | 18 | 2016-05-13 | sale | NULL | 3000.29 | 402 | 2 | | 9 | 丁丁 | female | 18 | 2011-03-12 | sale | NULL | 1000.37 | 402 | 2 | | 5 | jinxin | male | 18 | 1900-03-01 | teacher | NULL | 30000.00 | 401 | 1 | | 4 | jingliyang | female | 18 | 2011-02-11 | teacher | NULL | 9000.00 | 401 | 1 | | 12 | 张野 | male | 28 | 2016-03-11 | operation | NULL | 10000.13 | 403 | 3 | | 3 | liwenzhou | male | 28 | 2012-11-01 | teacher | NULL | 2100.00 | 401 | 1 | | 11 | 格格 | female | 28 | 2017-01-27 | sale | NULL | 4000.33 | 402 | 2 | | 8 | 丫丫 | female | 38 | 2010-11-01 | sale | NULL | 2000.35 | 402 | 2 | | 7 | 歪歪 | female | 48 | 2015-03-11 | sale | NULL | 3000.13 | 402 | 2 | | 6 | 成龙 | male | 48 | 2010-11-11 | teacher | NULL | 10000.00 | 401 | 1 | | 2 | yuanhao | male | 73 | 2014-07-01 | teacher | NULL | 3500.00 | 401 | 1 | | 1 | alex | male | 78 | 2015-03-02 | teacher | NULL | 1000000.31 | 401 | 1 | +----+------------+--------+-----+------------+-----------+--------------+------------+--------+-----------+ 16 rows in set (0.00 sec)
降序 desc
mysql> select * from employee order by age desc; +----+------------+--------+-----+------------+-----------+--------------+------------+--------+-----------+ | id | name | sex | age | hire_date | post | post_comment | salary | office | depart_id | +----+------------+--------+-----+------------+-----------+--------------+------------+--------+-----------+ | 1 | alex | male | 78 | 2015-03-02 | teacher | NULL | 1000000.31 | 401 | 1 | | 2 | yuanhao | male | 73 | 2014-07-01 | teacher | NULL | 3500.00 | 401 | 1 | | 7 | 歪歪 | female | 48 | 2015-03-11 | sale | NULL | 3000.13 | 402 | 2 | | 6 | 成龙 | male | 48 | 2010-11-11 | teacher | NULL | 10000.00 | 401 | 1 | | 8 | 丫丫 | female | 38 | 2010-11-01 | sale | NULL | 2000.35 | 402 | 2 | | 3 | liwenzhou | male | 28 | 2012-11-01 | teacher | NULL | 2100.00 | 401 | 1 | | 12 | 张野 | male | 28 | 2016-03-11 | operation | NULL | 10000.13 | 403 | 3 | | 11 | 格格 | female | 28 | 2017-01-27 | sale | NULL | 4000.33 | 402 | 2 | | 14 | 程咬银 | female | 18 | 2013-03-11 | operation | NULL | 19000.00 | 403 | 3 | | 15 | 程咬铜 | male | 18 | 2015-04-11 | operation | NULL | 18000.00 | 403 | 3 | | 13 | 程咬金 | male | 18 | 1997-03-12 | operation | NULL | 20000.00 | 403 | 3 | | 10 | 星星 | female | 18 | 2016-05-13 | sale | NULL | 3000.29 | 402 | 2 | | 9 | 丁丁 | female | 18 | 2011-03-12 | sale | NULL | 1000.37 | 402 | 2 | | 5 | jinxin | male | 18 | 1900-03-01 | teacher | NULL | 30000.00 | 401 | 1 | | 4 | jingliyang | female | 18 | 2011-02-11 | teacher | NULL | 9000.00 | 401 | 1 | | 16 | 程咬铁 | female | 18 | 2014-05-12 | operation | NULL | 17000.00 | 403 | 3 | +----+------------+--------+-----+------------+-----------+--------------+------------+--------+-----------+ 16 rows in set (0.00 sec)
按多列排序:先按照age升序 排序,如果年龄age相同情况下,年龄分不出胜负情况下 ,则按照id 降序排序
mysql> select * from employee order by age asc,id desc; +----+------------+--------+-----+------------+-----------+--------------+------------+--------+-----------+ | id | name | sex | age | hire_date | post | post_comment | salary | office | depart_id | +----+------------+--------+-----+------------+-----------+--------------+------------+--------+-----------+ | 16 | 程咬铁 | female | 18 | 2014-05-12 | operation | NULL | 17000.00 | 403 | 3 | | 15 | 程咬铜 | male | 18 | 2015-04-11 | operation | NULL | 18000.00 | 403 | 3 | | 14 | 程咬银 | female | 18 | 2013-03-11 | operation | NULL | 19000.00 | 403 | 3 | | 13 | 程咬金 | male | 18 | 1997-03-12 | operation | NULL | 20000.00 | 403 | 3 | | 10 | 星星 | female | 18 | 2016-05-13 | sale | NULL | 3000.29 | 402 | 2 | | 9 | 丁丁 | female | 18 | 2011-03-12 | sale | NULL | 1000.37 | 402 | 2 | | 5 | jinxin | male | 18 | 1900-03-01 | teacher | NULL | 30000.00 | 401 | 1 | | 4 | jingliyang | female | 18 | 2011-02-11 | teacher | NULL | 9000.00 | 401 | 1 | | 12 | 张野 | male | 28 | 2016-03-11 | operation | NULL | 10000.13 | 403 | 3 | | 11 | 格格 | female | 28 | 2017-01-27 | sale | NULL | 4000.33 | 402 | 2 | | 3 | liwenzhou | male | 28 | 2012-11-01 | teacher | NULL | 2100.00 | 401 | 1 | | 8 | 丫丫 | female | 38 | 2010-11-01 | sale | NULL | 2000.35 | 402 | 2 | | 7 | 歪歪 | female | 48 | 2015-03-11 | sale | NULL | 3000.13 | 402 | 2 | | 6 | 成龙 | male | 48 | 2010-11-11 | teacher | NULL | 10000.00 | 401 | 1 | | 2 | yuanhao | male | 73 | 2014-07-01 | teacher | NULL | 3500.00 | 401 | 1 | | 1 | alex | male | 78 | 2015-03-02 | teacher | NULL | 1000000.31 | 401 | 1 | +----+------------+--------+-----+------------+-----------+--------------+------------+--------+-----------+ 16 rows in set (0.00 sec)
按单列排序
SELECT * FROM employee ORDER BY salary;
SELECT * FROM employee ORDER BY salary ASC;
SELECT * FROM employee ORDER BY salary DESC;
按多列排序:先按照age排序,如果年纪相同,则按照薪资排序
SELECT * from employee
ORDER BY age,
salary DESC;
1. 查询所有员工信息,先按照age升序排序,如果age相同则按照hire_date降序排序
select * from employee ORDER BY age asc,hire_date desc;
2. 查询各岗位平均薪资大于10000的岗位名、平均工资,结果按平均薪资升序排列
select post,avg(salary) from employee group by post having avg(salary) > 10000 order by avg(salary) asc;
+-----------+---------------+
| post | avg(salary) |
+-----------+---------------+
| operation | 16800.026000 |
| teacher | 151842.901429 |
+-----------+---------------+
3. 查询各岗位平均薪资大于10000的岗位名、平均工资,结果按平均薪资降序排列
select post,avg(salary) from employee group by post having avg(salary) > 10000 order by avg(salary) desc;
+-----------+---------------+
| post | avg(salary) |
+-----------+---------------+
| teacher | 151842.901429 |
| operation | 16800.026000 |
+-----------+---------------+
以上是关于MySQL 查询常用操作——排序 order by的主要内容,如果未能解决你的问题,请参考以下文章