133 MySQL single-table queries

First, the single-table queries

1.1 Syntax

  • Some record each check out, they are a table, but this table is the presence of memory
  • If you want to query record secondary use, you can check out these tables to record a nickname
  • Some record lookup table in the display data, we can set taken out of the field names in the table, and when the check directly behind the field as an alias, as follows
select ditinct 字段1 as 别名1,字段2 as 别名2,···· from 表名
                                where 条件
                                group by 字段名
                                having 筛选
                                order by 字段名
                                limit 限制条数

1.2 key priority

from : 找到表
where : 拿着where 后面的约束的条件,去表/文件中取出一些记录
group by : 将取出的某些记录进行分组,如果没有group by,则整体作为一组
select : 执行select
distinct : 对去取出的重复数据进行去重
having : 将分组的结果进行having过滤
order by : 将结果按条件排序
limit : 限制显示的记录条数

1.3 Note (emphasis)

  • A query sequence, can have a variety of screening conditions, the conditions must be screened in order to gradually upward

  • distinct little special (writing position), the kind of conditions can be incomplete
  • A condition can be deleted, but not out of order

1.4 a single table of test distinct deduplication

# 创建一个表
create table t1(
    id int,
    x int,
    y int
);
1.插入几条数据
insert into t1 values(1, 1, 1), (2, 1, 2), (3, 2, 2), (4, 2, 2);
############################cmd 图示
mysql> select * from t1;
+------+------+------+
| id   | x    | y    |
+------+------+------+
|    1 |    1 |    1 |
|    2 |    1 |    2 |
|    3 |    2 |    2 |
|    4 |    2 |    2 |
+------+------+------+

2.查询全部数据
select distinct * from t1; 
############################cmd 图示
mysql> select distinct * from t1;
+------+------+------+
| id   | x    | y    |
+------+------+------+
|    1 |    1 |    1 |
|    2 |    1 |    2 |
|    3 |    2 |    2 |
|    4 |    2 |    2 |
+------+------+------+
4 rows in set (0.00 sec)

3.查询结果中,把重复的数据去掉
select distinct x, y from t1
############################cmd 图示
mysql> select distinct x,y from t1;
+------+------+
| x    | y    |
+------+------+
|    1 |    1 |
|    1 |    2 |
|    2 |    2 |
+------+------+
3 rows in set (0.00 sec)

4.查询字段y,把y字段的重复值去掉
select distinct y from t1;  # 结果 1  2
############################cmd 图示
mysql> select distinct y from t1;
+------+
| y    |
+------+
|    1 |
|    2 |
+------+
2 rows in set (0.00 sec)
  • distinct对参与查询的所有字段,整体去重(所查的全部字段的值都相同,才认为是重复数据)

Second, the table records the query test

Prior to this, to mention a few commonly used functions

2.1 Common Functions

  • Stitching: concat (), concat_ws ()
  • Case: upper (), lower ()
  • Floating-point operations: ceil (), floor () |, round ()
  • Integer: direct operation

2.2 Data Preparation

Our staff table (emp) as an example:

Employees id: id: int

Employee Name: name: varchar

Employees Gender: gender: enum

Employees Age: age: int

Wages and salaries: salary: float

City staff is located: area: varcahr

Address where employees: addr: varchar

Staff department: dep: varchar

1.先创建员工表(emp)
create table emp(
    id int primary key auto_increment,
    name varchar(10) not null,
    sex enum('男','女') default '女',
    age int unsigned default 25,
    salary float default 0,
    area varchar(20) null default '中国',
    addr varchar(20) null default '上海',
    dep varchar(20) null default '咨询部'
);
########查看表结构
mysql> desc emp;
+--------+-------------------+------+-----+-----------+----------------+
| Field  | Type              | Null | Key | Default   | Extra          |
+--------+-------------------+------+-----+-----------+----------------+
| id     | int(11)           | NO   | PRI | NULL      | auto_increment |
| name   | varchar(10)       | NO   |     | NULL      |                |
| sex    | enum('男','女')   | YES  |     | 女        |                |
| age    | int(10) unsigned  | YES  |     | 25        |                |
| salary | float             | YES  |     | 0         |                |
| area   | varchar(20)       | YES  |     | 中国      |                |
| addr   | varchar(20)       | YES  |     | 上海      |                |
| dep    | varchar(20)       | YES  |     | 咨询部    |                |
+--------+-------------------+------+-----+-----------+----------------+


2.插入数据
insert into emp values
    (1, 'aaa', '男', 42, 10.5, '上海', '浦东', '教职部'),
    (2, 'bbb', '男', 38, 9.4, '山东', '济南', '教学部'),
    (3, 'ccc', '女', 30, 3.0, '江苏', '张家港', '教学部'),
    (4, 'ddd', '女', 28, 2.4, '广州', '广东', '教学部'),
    (5, 'eee', '男', 28, 2.4, '江苏', '苏州', '教学部'),
    (6, 'fff', '男', 18, 8.8, '中国', '黄浦', '咨询部'),
    (7, 'ggg', '男', 18, 8.8, '安徽', '宣城', '教学部'),
    (8, 'hhh', '男', 28, 9.8, '安徽', '巢湖', '教学部'),
    (9, 'iii', '女', 36, 1.2, '安徽', '芜湖', '咨询部'),
    (10, 'jjj', '男', 36, 5.8, '山东', '济南', '教学部'),
    (11, 'kkk', '女', 28, 1.2, '山东', '青岛', '教职部'),
    (12, 'lll', '男', 30, 9.0, '上海', '浦东', '咨询部'),
    (13, 'mmm', '男', 30, 6.0, '上海', '浦东', '咨询部'),
    (14, 'nnn', '男', 30, 6.0, '上海', '浦西', '教学部'),
    (15, 'ooo', '女', 67, 2.501, '上海', '陆家嘴', '教学部');
########查看表记录
mysql> select * from emp;
+----+------+------+------+--------+--------+-----------+-----------+
| id | name | sex  | age  | salary | area   | addr      | dep       |
+----+------+------+------+--------+--------+-----------+-----------+
|  1 | aaa  | 男   |   42 |   10.5 | 上海   | 浦东      | 教职部    |
|  2 | bbb  | 男   |   38 |    9.4 | 山东   | 济南      | 教学部    |
|  3 | ccc  | 女   |   30 |      3 | 江苏   | 张家港    | 教学部    |
|  4 | ddd  | 女   |   28 |    2.4 | 广州   | 广东      | 教学部    |
|  5 | eee  | 男   |   28 |    2.4 | 江苏   | 苏州      | 教学部    |
|  6 | fff  | 男   |   18 |    8.8 | 中国   | 黄浦      | 咨询部    |
|  7 | ggg  | 男   |   18 |    8.8 | 安徽   | 宣城      | 教学部    |
|  8 | hhh  | 男   |   28 |    9.8 | 安徽   | 巢湖      | 教学部    |
|  9 | iii  | 女   |   36 |    1.2 | 安徽   | 芜湖      | 咨询部    |
| 10 | jjj  | 男   |   36 |    5.8 | 山东   | 济南      | 教学部    |
| 11 | kkk  | 女   |   28 |    1.2 | 山东   | 青岛      | 教职部    |
| 12 | lll  | 男   |   30 |      9 | 上海   | 浦东      | 咨询部    |
| 13 | mmm  | 男   |   30 |      6 | 上海   | 浦东      | 咨询部    |
| 14 | nnn  | 男   |   30 |      6 | 上海   | 浦西      | 教学部    |
| 15 | ooo  | 女   |   67 |  2.501 | 上海   | 陆家嘴    | 教学部    |
+----+------+------+------+--------+--------+-----------+-----------+
15 rows in set (0.00 sec)

Table 2.3 Test record

2.3.1 where the query conditions

  • grammar

    select 字段1,字段2···· from 表名 where 条件表达式
  • Conditional rules

    1.比较符合  >   <   >=   <=   =    !=
    2.区间符合  between开始 and结束     in(自定义容器)
    3.逻辑符合  and    or   not
    4.相似符合  like _(下划线代表单个字符)   %(可以匹配多个)
    5.正则符合  regexp 正则语法
  • Data test case (to the staff table, for example)

    • Queries wage salary greater than 5 employees (more in line)

      mysql> select * from emp where salary >5;
      
      +----+------+------+------+--------+--------+--------+-----------+
      | id | name | sex  | age  | salary | area   | addr   | dep       |
      +----+------+------+------+--------+--------+--------+-----------+
      |  1 | aaa  | 男   |   42 |   10.5 | 上海   | 浦东   | 教职部    |
      |  2 | bbb  | 男   |   38 |    9.4 | 山东   | 济南   | 教学部    |
      |  6 | fff  | 男   |   18 |    8.8 | 中国   | 黄浦   | 咨询部    |
      |  7 | ggg  | 男   |   18 |    8.8 | 安徽   | 宣城   | 教学部    |
      |  8 | hhh  | 男   |   28 |    9.8 | 安徽   | 巢湖   | 教学部    |
      | 10 | jjj  | 男   |   36 |    5.8 | 山东   | 济南   | 教学部    |
      | 12 | lll  | 男   |   30 |      9 | 上海   | 浦东   | 咨询部    |
      | 13 | mmm  | 男   |   30 |      6 | 上海   | 浦东   | 咨询部    |
      | 14 | nnn  | 男   |   30 |      6 | 上海   | 浦西   | 教学部    |
      +----+------+------+------+--------+--------+--------+-----------+
    • Query id number is an even number of all employees (more in line)

      mysql> select * from emp where id%2=0;
      
      +----+------+------+------+--------+--------+--------+-----------+
      | id | name | sex  | age  | salary | area   | addr   | dep       |
      +----+------+------+------+--------+--------+--------+-----------+
      |  2 | bbb  | 男   |   38 |    9.4 | 山东   | 济南   | 教学部    |
      |  4 | ddd  | 女   |   28 |    2.4 | 广州   | 广东   | 教学部    |
      |  6 | fff  | 男   |   18 |    8.8 | 中国   | 黄浦   | 咨询部    |
      |  8 | hhh  | 男   |   28 |    9.8 | 安徽   | 巢湖   | 教学部    |
      | 10 | jjj  | 男   |   36 |    5.8 | 山东   | 济南   | 教学部    |
      | 12 | lll  | 男   |   30 |      9 | 上海   | 浦东   | 咨询部    |
      | 14 | nnn  | 男   |   30 |      6 | 上海   | 浦西   | 教学部    |
      +----+------+------+------+--------+--------+--------+-----------+
    • All staff queries wages between 6 and 9 (range in line with between and)

      mysql> select * from emp where id between 6 and 9;
      
      +----+------+------+------+--------+--------+--------+-----------+
      | id | name | sex  | age  | salary | area   | addr   | dep       |
      +----+------+------+------+--------+--------+--------+-----------+
      |  6 | fff  | 男   |   18 |    8.8 | 中国   | 黄浦   | 咨询部    |
      |  7 | ggg  | 男   |   18 |    8.8 | 安徽   | 宣城   | 教学部    |
      |  8 | hhh  | 男   |   28 |    9.8 | 安徽   | 巢湖   | 教学部    |
      |  9 | iii  | 女   |   36 |    1.2 | 安徽   | 芜湖   | 咨询部    |
      +----+------+------+------+--------+--------+--------+-----------+
    • Queries wages in 1, 3, 7, between 20 (in line with the interval, custom containers in)

      mysql> select * from emp where id in(1,3,7,20);
      
      +----+------+------+------+--------+--------+-----------+-----------+
      | id | name | sex  | age  | salary | area   | addr      | dep       |
      +----+------+------+------+--------+--------+-----------+-----------+
      |  1 | aaa  | 男   |   42 |   10.5 | 上海   | 浦东      | 教职部    |
      |  3 | ccc  | 女   |   30 |      3 | 江苏   | 张家港    | 教学部    |
      |  7 | ggg  | 男   |   18 |    8.8 | 安徽   | 宣城      | 教学部    |
      +----+------+------+------+--------+--------+-----------+-----------+
    • Queries with the names of all employees o characters (similar accord like%)

      mysql> select * from emp where name like '%o%';
      
      +----+------+------+------+--------+--------+-----------+-----------+
      | id | name | sex  | age  | salary | area   | addr      | dep       |
      +----+------+------+------+--------+--------+-----------+-----------+
      | 15 | ooo  | 女   |   67 |  2.501 | 上海   | 陆家嘴    | 教学部    |
      +----+------+------+------+--------+--------+-----------+-----------+
    • All queries were second character is m employees ** (in line with similar like _) **

      mysql> select * from emp where name like '_o%';
      
      +----+------+------+------+--------+--------+-----------+-----------+
      | id | name | sex  | age  | salary | area   | addr      | dep       |
      +----+------+------+------+--------+--------+-----------+-----------+
      | 15 | ooo  | 女   |   67 |  2.501 | 上海   | 陆家嘴    | 教学部    |
    • All the names of the two characters before the query is a staff ** (in line with similar like __) **

      mysql> select * from emp where name like '__a%';
      
      +----+------+------+------+--------+--------+--------+-----------+
      | id | name | sex  | age  | salary | area   | addr   | dep       |
      +----+------+------+------+--------+--------+--------+-----------+
      |  1 | aaa  | 男   |   42 |   10.5 | 上海   | 浦东   | 教职部    |
      +----+------+------+------+--------+--------+--------+-----------+
    • Query id numbers of all employees containing all 1's (regular match)

      • sql support only part of the regular grammar
      • '. * \ D'; does not support \ d represents the number that \ d
      • '. * [0-9]'; support [] syntax
      mysql> select * from emp where id regexp '.*[1]';
      
      +----+------+------+------+--------+--------+-----------+-----------+
      | id | name | sex  | age  | salary | area   | addr      | dep       |
      +----+------+------+------+--------+--------+-----------+-----------+
      |  1 | aaa  | 男   |   42 |   10.5 | 上海   | 浦东      | 教职部    |
      | 10 | jjj  | 男   |   36 |    5.8 | 山东   | 济南      | 教学部    |
      | 11 | kkk  | 女   |   28 |    1.2 | 山东   | 青岛      | 教职部    |
      | 12 | lll  | 男   |   30 |      9 | 上海   | 浦东      | 咨询部    |
      | 13 | mmm  | 男   |   30 |      6 | 上海   | 浦东      | 咨询部    |
      | 14 | nnn  | 男   |   30 |      6 | 上海   | 浦西      | 教学部    |
      | 15 | ooo  | 女   |   67 |  2.501 | 上海   | 陆家嘴    | 教学部    |
      +----+------+------+------+--------+--------+-----------+-----------+

2.3.2 group by grouping queries

First, the problem of grouping query

  • 1. sql_mode no restrictions under ONLY_FULL_GROUP_BY, can be performed, but the result does not make sense

    • After grouping, the data in Table consideration is not a single record, because each packet contains a plurality of records, reference packet fields, many records in each packet unitary, the following tests described above
    # 按部门分组,每个部门都有哪些人
    mysql> select * from emp group by dep;
    +----+------+------+------+--------+--------+--------+-----------+
    | id | name | sex  | age  | salary | area   | addr   | dep       |
    +----+------+------+------+--------+--------+--------+-----------+
    |  6 | fff  | 男   |   18 |    8.8 | 中国   | 黄浦   | 咨询部    |
    |  2 | bbb  | 男   |   38 |    9.4 | 山东   | 济南   | 教学部    |
    |  1 | aaa  | 男   |   42 |   10.5 | 上海   | 浦东   | 教职部    |
    +----+------+------+------+--------+--------+--------+-----------+
  • 2. ONLY_FULL_GROUP_BY limit, an error

    • Configure the following code in the database configuration file my.ini, and then restart the service

      sql_mode=ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION

    # 重启服务后重新进行如上的分组测试
    1.会发现直接报错,因为我们有了这个分组限制以后,那么select查询的字段只能是分组的那个字段dep
    mysql>  select dep from emp group by dep;
    
    +-----------+
    | dep       |
    +-----------+
    | 咨询部    |
    | 教学部    |
    | 教职部    |
    +-----------+

Second, aggregate functions

Because we have this grouping stint, then the field is that only a select query field dep packets, and the value of his day table fields are not obtain, so we have this grouping is not the result we wanted, so I want to to obtain additional information within a group, we need the help function

1. Function Classification polymerization
最大值  max()
最小值  min()
平均值  avg()
求和    sum()
计数    count()
组内字段拼接,用来查看组内其他字段   group_concat()
2. Data Test (to employee table, for example)
  • According to sector groups, and view the members of the group name (group_concat ())

    mysql> select dep,group_concat(name) from emp group by dep;
    
    +-----------+-------------------------------------+
    | dep       | group_concat(name)                  |
    +-----------+-------------------------------------+
    | 咨询部    | mmm,lll,fff,iii                     |
    | 教学部    | ooo,nnn,jjj,hhh,ggg,eee,ddd,ccc,bbb |
    | 教职部    | kkk,aaa                             |
    +-----------+-------------------------------------+
  • Grouped by sector, each department who has the highest salary, the minimum salary, average salary, set in a total number of people

    mysql> select
        -> dep 部门,
        -> group_concat(name) 成员,
        -> max(salary) 最高薪资,
        -> min(salary) 最低薪资,
        -> avg(salary) 平均薪资,
        -> sum(salary) 总薪资,
        -> count(sex) 人数
        -> from emp group by dep;
  • Grouped by sector, see the maximum age

    mysql> select dep 部门,max(age) 最高年龄 from emp group by dep;
    
    +-----------+--------------+
    | 部门      | 最高年龄     |
    +-----------+--------------+
    | 咨询部    |           36 |
    | 教学部    |           67 |
    | 教职部    |           42 |
    +-----------+--------------+

2.3.3 having 和 where

  • In the absence of a packet, the same result where the having

  • Key: having polymerization can filter the results

    # 没有分组时where和having测试
    mysql> select * from emp where id in (5, 10, 15, 20);
    +----+------+------+------+--------+--------+-----------+-----------+
    | id | name | sex  | age  | salary | area   | addr      | dep       |
    +----+------+------+------+--------+--------+-----------+-----------+
    |  5 | eee  | 男   |   28 |    2.4 | 江苏   | 苏州      | 教学部    |
    | 10 | jjj  | 男   |   36 |    5.8 | 山东   | 济南      | 教学部    |
    | 15 | ooo  | 女   |   67 |  2.501 | 上海   | 陆家嘴    | 教学部    |
    +----+------+------+------+--------+--------+-----------+-----------+
    
    
    mysql> select * from emp having salary > 5;# 报错了,这里因为我之前配置了数据库的文件
    ERROR 1463 (42000): Non-grouping field 'salary' is used in HAVING clause
  • having grouped screening test

    • Grouped by sector, for all to see, the highest salary, the minimum salary, average salary, among the total number of minimum wage is less than 2

      mysql> select
          -> dep 部门,
          -> group_concat(name) 成员,
          -> max(salary) 最高薪资,
          -> min(salary) 最低薪资,
          -> avg(salary) 平均薪资,
          -> sum(salary) 总薪资,
          -> count(sex) 人数
          -> from emp group by dep having min(salary)<2;
      # 由于工资是小数,所以在数据库里可能会出现一些错误
      +-----------+-----------------+--------------+--------------------+---------------
      | 部门  | 成员            |最高薪资| 最低薪资| 平均薪资| 总薪资 |人数|
      +-----------+-----------------+--------------+--------------------+---------------
      | 咨询部| mmm,lll,fff,iii |9      | 1.20  | 6.250  | 25.0  | 4 |
      | 教职部| kkk,aaa         |10.5   | 1.20  | 5.85   | 11.7  | 2 |
      +-----------+-----------------+--------------+--------------------+---------------

2.3.4 sort order by

First, the rules of grammar

asc : 字段升序排序,默认是升序
desc : 字段降序排序

# 语法
order by 主排序字段 [asc|desc], 次排序字段1 [asc|desc], ...次排序字段n [asc|desc]
#1.先按主排序字段排序,如果出现主排序字段有某两个或多个字段相同,那就再去按次排序字段排序

Second, the test data

  • Testing the ungrouped

    • By age ascending

      mysql> select * from emp order by age asc;
      +----+------+------+------+--------+--------+-----------+-----------+
      | id | name | sex  | age  | salary | area   | addr      | dep       |
      +----+------+------+------+--------+--------+-----------+-----------+
      |  7 | ggg  | 男   |   18 |    8.8 | 安徽   | 宣城      | 教学部    |
      |  6 | fff  | 男   |   18 |    8.8 | 中国   | 黄浦      | 咨询部    |
      | 11 | kkk  | 女   |   28 |    1.2 | 山东   | 青岛      | 教职部    |
      |  8 | hhh  | 男   |   28 |    9.8 | 安徽   | 巢湖      | 教学部    |
      |  5 | eee  | 男   |   28 |    2.4 | 江苏   | 苏州      | 教学部    |
      |  4 | ddd  | 女   |   28 |    2.4 | 广州   | 广东      | 教学部    |
      |  3 | ccc  | 女   |   30 |      3 | 江苏   | 张家港    | 教学部    |
      | 14 | nnn  | 男   |   30 |      6 | 上海   | 浦西      | 教学部    |
      | 12 | lll  | 男   |   30 |      9 | 上海   | 浦东      | 咨询部    |
      | 13 | mmm  | 男   |   30 |      6 | 上海   | 浦东      | 咨询部    |
      |  9 | iii  | 女   |   36 |    1.2 | 安徽   | 芜湖      | 咨询部    |
      | 10 | jjj  | 男   |   36 |    5.8 | 山东   | 济南      | 教学部    |
      |  2 | bbb  | 男   |   38 |    9.4 | 山东   | 济南      | 教学部    |
      |  1 | aaa  | 男   |   42 |   10.5 | 上海   | 浦东      | 教职部    |
      | 15 | ooo  | 女   |   67 |  2.501 | 上海   | 陆家嘴    | 教学部    |
      +----+------+------+------+--------+--------+-----------+-----------+
    • In descending order according to salary

      mysql> select * from emp order by salary desc;
      +----+------+------+------+--------+--------+-----------+-----------+
      | id | name | sex  | age  | salary | area   | addr      | dep       |
      +----+------+------+------+--------+--------+-----------+-----------+
      |  1 | aaa  | 男   |   42 |   10.5 | 上海   | 浦东      | 教职部    |
      |  8 | hhh  | 男   |   28 |    9.8 | 安徽   | 巢湖      | 教学部    |
      |  2 | bbb  | 男   |   38 |    9.4 | 山东   | 济南      | 教学部    |
      | 12 | lll  | 男   |   30 |      9 | 上海   | 浦东      | 咨询部    |
      |  6 | fff  | 男   |   18 |    8.8 | 中国   | 黄浦      | 咨询部    |
      |  7 | ggg  | 男   |   18 |    8.8 | 安徽   | 宣城      | 教学部    |
      | 14 | nnn  | 男   |   30 |      6 | 上海   | 浦西      | 教学部    |
      | 13 | mmm  | 男   |   30 |      6 | 上海   | 浦东      | 咨询部    |
      | 10 | jjj  | 男   |   36 |    5.8 | 山东   | 济南      | 教学部    |
      |  3 | ccc  | 女   |   30 |      3 | 江苏   | 张家港    | 教学部    |
      | 15 | ooo  | 女   |   67 |  2.501 | 上海   | 陆家嘴    | 教学部    |
      |  5 | eee  | 男   |   28 |    2.4 | 江苏   | 苏州      | 教学部    |
      |  4 | ddd  | 女   |   28 |    2.4 | 广州   | 广东      | 教学部    |
      | 11 | kkk  | 女   |   28 |    1.2 | 山东   | 青岛      | 教职部    |
      |  9 | iii  | 女   |   36 |    1.2 | 安徽   | 芜湖      | 咨询部    |
      +----+------+------+------+--------+--------+-----------+-----------+
    • By salary in descending order, if the same, then in descending order of age

      mysql> select * from emp order by salary desc,age desc;
      +----+------+------+------+--------+--------+-----------+-----------+
      | id | name | sex  | age  | salary | area   | addr      | dep       |
      +----+------+------+------+--------+--------+-----------+-----------+
      |  1 | aaa  | 男   |   42 |   10.5 | 上海   | 浦东      | 教职部    |
      |  8 | hhh  | 男   |   28 |    9.8 | 安徽   | 巢湖      | 教学部    |
      |  2 | bbb  | 男   |   38 |    9.4 | 山东   | 济南      | 教学部    |
      | 12 | lll  | 男   |   30 |      9 | 上海   | 浦东      | 咨询部    |
      |  6 | fff  | 男   |   18 |    8.8 | 中国   | 黄浦      | 咨询部    |
      |  7 | ggg  | 男   |   18 |    8.8 | 安徽   | 宣城      | 教学部    |
      | 14 | nnn  | 男   |   30 |      6 | 上海   | 浦西      | 教学部    |
      | 13 | mmm  | 男   |   30 |      6 | 上海   | 浦东      | 咨询部    |
      | 10 | jjj  | 男   |   36 |    5.8 | 山东   | 济南      | 教学部    |
      |  3 | ccc  | 女   |   30 |      3 | 江苏   | 张家港    | 教学部    |
      | 15 | ooo  | 女   |   67 |  2.501 | 上海   | 陆家嘴    | 教学部    |
      |  5 | eee  | 男   |   28 |    2.4 | 江苏   | 苏州      | 教学部    |
      |  4 | ddd  | 女   |   28 |    2.4 | 广州   | 广东      | 教学部    |
      |  9 | iii  | 女   |   36 |    1.2 | 安徽   | 芜湖      | 咨询部    |
      | 11 | kkk  | 女   |   28 |    1.2 | 山东   | 青岛      | 教职部    |
      +----+------+------+------+--------+--------+-----------+-----------+
    • In descending order according to age, if the same, then pay Descending

      mysql> select * from emp order by age desc,salary desc;
      +----+------+------+------+--------+--------+-----------+-----------+
      | id | name | sex  | age  | salary | area   | addr      | dep       |
      +----+------+------+------+--------+--------+-----------+-----------+
      | 15 | ooo  | 女   |   67 |  2.501 | 上海   | 陆家嘴    | 教学部    |
      |  1 | aaa  | 男   |   42 |   10.5 | 上海   | 浦东      | 教职部    |
      |  2 | bbb  | 男   |   38 |    9.4 | 山东   | 济南      | 教学部    |
      | 10 | jjj  | 男   |   36 |    5.8 | 山东   | 济南      | 教学部    |
      |  9 | iii  | 女   |   36 |    1.2 | 安徽   | 芜湖      | 咨询部    |
      | 12 | lll  | 男   |   30 |      9 | 上海   | 浦东      | 咨询部    |
      | 14 | nnn  | 男   |   30 |      6 | 上海   | 浦西      | 教学部    |
      | 13 | mmm  | 男   |   30 |      6 | 上海   | 浦东      | 咨询部    |
      |  3 | ccc  | 女   |   30 |      3 | 江苏   | 张家港    | 教学部    |
      |  8 | hhh  | 男   |   28 |    9.8 | 安徽   | 巢湖      | 教学部    |
      |  5 | eee  | 男   |   28 |    2.4 | 江苏   | 苏州      | 教学部    |
      |  4 | ddd  | 女   |   28 |    2.4 | 广州   | 广东      | 教学部    |
      | 11 | kkk  | 女   |   28 |    1.2 | 山东   | 青岛      | 教职部    |
      |  7 | ggg  | 男   |   18 |    8.8 | 安徽   | 宣城      | 教学部    |
      |  6 | fff  | 男   |   18 |    8.8 | 中国   | 黄浦      | 咨询部    |
      +----+------+------+------+--------+--------+-----------+-----------+
  • Grouped state

    • According to the highest salary in descending order (after already above group by group, for example)

      mysql> select
          -> dep 部门,
          -> group_concat(name)成员,
          -> max(salary) 最高薪资,
          -> min(salary) 最低薪资,
          -> avg(salary) 平均薪资,
          -> sum(salary) 总薪资,
          -> count(sex) 人数
          -> from emp group by dep order by 最高薪资 desc;
      
      # 由于工资是小数,所以在数据库里可能会出现一些错误
      +-----------+-------------------------------------+--------------+--------------+-
      | 部门 | 成员                                 |最高薪资| 最低薪资|平均薪资|总薪资|人数   |
      +-----------+-------------------------------------+--------------+--------------+-
      | 教职部| kkk,aaa                             |10.5  |1.2     |5.85   |11.7 |2 |
      | 教学部| ooo,nnn,jjj,hhh,ggg,eee,ddd,ccc,bbb |9.8   |2.4     |5.56   |50.1 |9 |
      | 咨询部| mmm,lll,fff,iii                     |9     |1.2     |6.2    |25.0 |4 |
      +-----------+-------------------------------------+--------------+--------------+-

2.3.5 limit restrictions

First, grammar

limit 条数 
limit 偏移量,条数 偏移量就是跳过几条数据后开始取

Second, the test data (restrictions limit)

  • Salaries and wages employee name is less than 8, the decreasing order of wages, a selected

     select name,salary from emp where salary<8 order by salary desc limit 1;
    +------+--------+
    | name | salary |
    +------+--------+
    | mmm  |      6 |
    +------+--------+
  • All employee data table queries, satisfied to shift the recording condition 5, then check 3

    mysql> select * from emp limit 5,3;
    +----+------+------+------+--------+--------+--------+-----------+
    | id | name | sex  | age  | salary | area   | addr   | dep       |
    +----+------+------+------+--------+--------+--------+-----------+
    |  6 | fff  | 男   |   18 |    8.8 | 中国   | 黄浦   | 咨询部    |
    |  7 | ggg  | 男   |   18 |    8.8 | 安徽   | 宣城   | 教学部    |
    |  8 | hhh  | 男   |   28 |    9.8 | 安徽   | 巢湖   | 教学部    |
    +----+------+------+------+--------+--------+--------+-----------+

Guess you like

Origin www.cnblogs.com/xichenHome/p/11588406.html