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 |
+-----------+---------------+
 
 
 

原文地址:https://www.cnblogs.com/mingerlcm/p/9880137.html

时间: 2024-10-14 05:12:05

mysql 数据操作 单表查询 查询排序: order by的相关文章

mysql 数据操作 单表查询 通过四则运算查询

#通过四则运算查询 SELECT name, salary*12 FROM employee; SELECT name, salary*12 AS Annual_salary FROM employee; SELECT name, salary*12 Annual_salary FROM employee; 查看年薪salary*12 mysql> select name,salary*12 from employee; +------------+-------------+ | name |

mysql 数据操作 单表查询 简单查询 避免重复DISTINCT

创建数据库company create database company charset=utf8; use company; company.employee 员工id id int 姓名 emp_name varchar 性别 sex enum 年龄 age int 入职日期 hire_date date 岗位 post varchar 职位描述 post_comment varchar 薪水 salary double 办公室 office int 部门编号 depart_id int #

mysql 数据操作 单表查询 group by 练习

1. 查询岗位名以及岗位包含的所有员工名字 mysql> select post,group_concat(name) from employee group by post; +-----------+-------------------------------------------------+ | post | group_concat(name) | +-----------+-------------------------------------------------+ | o

mysql 数据操作 单表查询 having 过滤 练习

1. 查询各岗位内包含的员工个数小于2的岗位名.岗位内包含员工名字.个数 mysql> select post,group_concat(name),count(id) from employee group by post; +-----------+-------------------------------------------------+-----------+ | post | group_concat(name) | count(id) | +-----------+-----

mysql 数据操作 单表查询 limit 练习

1. 分页显示,每页5条 mysql> select * from employee limit 0,5; +----+------------+--------+-----+------------+---------+--------------+------------+--------+-----------+ | id | name | sex | age | hire_date | post | post_comment | salary | office | depart_id |

mysql 数据操作 多表查询 目录

mysql 数据操作 多表查询 准备 mysql 数据操作 多表查询 多表连接查询 笛卡尔积 mysql 数据操作 多表查询 多表连接查询 内连接 mysql 数据操作 多表查询 多表连接查询 外链接之左连接 右连接 mysql 数据操作 多表查询 多表连接查询 全外连接 原文地址:https://www.cnblogs.com/mingerlcm/p/10523097.html

数据库:数据操作-单表查询

一 单表查询的语法 select 字段1,字段2....from 表名 where 条件 group by field having 筛选 order by field limit 限制条数 二 关键字的执行优先级(重点) 关键字的执行优先级 from where group by having select distinct order by limit 1.找到表:from 2.拿着where指定的约束条件,去文件/表中取出一条条记录 3.将取出的一条条记录进行分组group by,如果没有

MySQL DML操作--------多表联合查询实战

1. 背景 * 多表联合查询是把不同表的记录到一起的一种方式 * 在SQL标准中规划的联合(join)大致分内连接,外连接,全连接.其中外连接又分左外连接,右外连接. 2. 内连接例子 (inner join) [ 员工 --> 部门 ] * 查看员工表[ employees ]和部门表[ departments ]结构 mysql> desc employees; +-----------+---------------+------+-----+---------+------------

mysql简单的单表查询详解

mysql简单的单表查询详解 MySQL的查询操作: 单表查询:简单查询 多表查询:连续查询 联合查询: 选择和投影: 投影:挑选要显示的字段 选择:挑选符合条件的行 投影:SELECT 字段1, 字段2, ... FROM tb_name;  SELECT * FROM tb_name; 选择:SELECT 字段1, ... FROM tb_name WHERE 子句; 布尔条件表达式 mysql> CREATE TABLE students (SID INT UNSIGNED AUTO_IN