##########################

 

一、先排序后分组问题:

 

数据准备:

[work@xxx scripts]$ mysql  -hyyy -P3306  -uroot -proot
 WARNING: DATABASE IS WRITEABE MAY BE MASTER, BE CAREFUL 
Warning: Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 111044039
Server version: 5.5.31-log MySQL Community Server (GPL)

Copyright (c) 2009-2015 Percona LLC and/or its affiliates
Copyright (c) 2000, 2015, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

Mon Feb 22 18:40:09 2021
root@xxx((none)) > use apple
Database changed
Mon Feb 22 18:40:16 2021
root@xxx(apple) > show create table t_user\G
*************************** 1. row ***************************
       Table: t_user
Create Table: CREATE TABLE `t_user` (
  `id` bigint(20) NOT NULL AUTO_INCREMENT,
  `name` varchar(255) DEFAULT NULL,
  `phone` varchar(255) DEFAULT NULL,
  `gender` varchar(255) DEFAULT NULL,
  `type` varchar(255) DEFAULT NULL,
  `birth` datetime DEFAULT NULL,
  `is_delete` varchar(255) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=101 DEFAULT CHARSET=utf8
1 row in set (0.00 sec)

Mon Feb 22 18:40:46 2021
######################

---插入数据

INSERT INTO `t_user` VALUES ('1', 'James', '0594-5397864', '0', '3', '2016-01-30 19:01:09', '1'); INSERT INTO `t_user` VALUES ('2', 'Hayes', '0594-5392419', '1', '4', '2015-12-24 11:12:27', '1'); INSERT INTO `t_user` VALUES ('3', 'Diana', '0594-5393520', '1', '5', '2016-03-21 13:03:50', '0'); INSERT INTO `t_user` VALUES ('4', 'Rajah', '0594-5399812', '1', '4', '2015-11-26 02:11:35', '0'); INSERT INTO `t_user` VALUES ('5', 'Daria', '0594-5397571', '0', '4', '2016-01-18 11:01:11', '1'); INSERT INTO `t_user` VALUES ('6', 'Lee', '0594-5394539', '1', '1', '2015-10-23 08:10:23', '1'); INSERT INTO `t_user` VALUES ('7', 'Cameran', '0594-5392867', '0', '4', '2016-11-16 12:11:08', '0'); INSERT INTO `t_user` VALUES ('8', 'Wylie', '0594-5395349', '0', '5', '2017-07-06 04:07:27', '0'); INSERT INTO `t_user` VALUES ('9', 'Bertha', '0594-5395287', '1', '1', '2017-02-08 12:02:45', '1'); INSERT INTO `t_user` VALUES ('10', 'Fletcher', '0594-5399246', '0', '4', '2015-09-03 20:09:33', '0'); INSERT INTO `t_user` VALUES ('11', 'Conan', '0594-5391546', '1', '5', '2017-05-15 09:05:23', '0'); INSERT INTO `t_user` VALUES ('12', 'Raymond', '0594-5399666', '0', '3', '2015-10-20 05:10:05', '1'); INSERT INTO `t_user` VALUES ('13', 'Noel', '0594-5397392', '1', '4', '2017-05-26 03:05:56', '0'); INSERT INTO `t_user` VALUES ('14', 'Miriam', '0594-5399081', '0', '2', '2016-05-21 02:05:09', '0'); INSERT INTO `t_user` VALUES ('15', 'Maya', '0594-5397242', '0', '3', '2016-10-24 02:10:50', '1'); INSERT INTO `t_user` VALUES ('16', 'Winifred', '0594-5395142', '1', '1', '2017-03-15 02:03:43', '0'); INSERT INTO `t_user` VALUES ('17', 'Elaine', '0594-5398478', '1', '3', '2017-03-08 15:03:03', '1'); INSERT INTO `t_user` VALUES ('18', 'Robert', '0594-5397830', '0', '5', '2016-02-10 22:02:06', '0'); INSERT INTO `t_user` VALUES ('19', 'Patrick', '0594-5396516', '0', '4', '2015-09-10 07:09:51', '0'); INSERT INTO `t_user` VALUES ('20', 'Darrel', '0594-5397417', '0', '1', '2016-03-11 11:03:36', '0'); INSERT INTO `t_user` VALUES ('21', 'Salvador', '0594-5399732', '1', '3', '2016-01-01 15:01:21', '0'); INSERT INTO `t_user` VALUES ('22', 'Brandon', '0594-5396204', '1', '4', '2016-05-12 06:05:40', '1'); INSERT INTO `t_user` VALUES ('23', 'Dorothy', '0594-5396783', '0', '1', '2016-12-12 10:12:59', '1'); INSERT INTO `t_user` VALUES ('24', 'Kevyn', '0594-5398240', '0', '2', '2016-02-07 04:02:14', '1'); INSERT INTO `t_user` VALUES ('25', 'Brody', '0594-5398774', '1', '1', '2016-12-11 20:12:36', '0');

 

mysql5.5上执行:

root@xxx(apple) > select t1.* from t_user t1;
+----+----------+--------------+--------+------+---------------------+-----------+
| id | name     | phone        | gender | type | birth               | is_delete |
+----+----------+--------------+--------+------+---------------------+-----------+
|  1 | James    | 0594-5397864 | 0      | 3    | 2016-01-30 19:01:09 | 1         |
|  2 | Hayes    | 0594-5392419 | 1      | 4    | 2015-12-24 11:12:27 | 1         |
|  3 | Diana    | 0594-5393520 | 1      | 5    | 2016-03-21 13:03:50 | 0         |
|  4 | Rajah    | 0594-5399812 | 1      | 4    | 2015-11-26 02:11:35 | 0         |
|  5 | Daria    | 0594-5397571 | 0      | 4    | 2016-01-18 11:01:11 | 1         |
|  6 | Lee      | 0594-5394539 | 1      | 1    | 2015-10-23 08:10:23 | 1         |
|  7 | Cameran  | 0594-5392867 | 0      | 4    | 2016-11-16 12:11:08 | 0         |
|  8 | Wylie    | 0594-5395349 | 0      | 5    | 2017-07-06 04:07:27 | 0         |
|  9 | Bertha   | 0594-5395287 | 1      | 1    | 2017-02-08 12:02:45 | 1         |
| 10 | Fletcher | 0594-5399246 | 0      | 4    | 2015-09-03 20:09:33 | 0         |
| 11 | Conan    | 0594-5391546 | 1      | 5    | 2017-05-15 09:05:23 | 0         |
| 12 | Raymond  | 0594-5399666 | 0      | 3    | 2015-10-20 05:10:05 | 1         |
| 13 | Noel     | 0594-5397392 | 1      | 4    | 2017-05-26 03:05:56 | 0         |
| 14 | Miriam   | 0594-5399081 | 0      | 2    | 2016-05-21 02:05:09 | 0         |
| 15 | Maya     | 0594-5397242 | 0      | 3    | 2016-10-24 02:10:50 | 1         |
| 16 | Winifred | 0594-5395142 | 1      | 1    | 2017-03-15 02:03:43 | 0         |
| 17 | Elaine   | 0594-5398478 | 1      | 3    | 2017-03-08 15:03:03 | 1         |
| 18 | Robert   | 0594-5397830 | 0      | 5    | 2016-02-10 22:02:06 | 0         |
| 19 | Patrick  | 0594-5396516 | 0      | 4    | 2015-09-10 07:09:51 | 0         |
| 20 | Darrel   | 0594-5397417 | 0      | 1    | 2016-03-11 11:03:36 | 0         |
| 21 | Salvador | 0594-5399732 | 1      | 3    | 2016-01-01 15:01:21 | 0         |
| 22 | Brandon  | 0594-5396204 | 1      | 4    | 2016-05-12 06:05:40 | 1         |
| 23 | Dorothy  | 0594-5396783 | 0      | 1    | 2016-12-12 10:12:59 | 1         |
| 24 | Kevyn    | 0594-5398240 | 0      | 2    | 2016-02-07 04:02:14 | 1         |
| 25 | Brody    | 0594-5398774 | 1      | 1    | 2016-12-11 20:12:36 | 0         |
+----+----------+--------------+--------+------+---------------------+-----------+
25 rows in set (0.00 sec)

Mon Feb 22 18:41:00 2021
root@xxx(apple) > select t2.* from (select t1.* from t_user t1 order by t1.id desc) as t2 group by t2.type;
+----+----------+--------------+--------+------+---------------------+-----------+
| id | name     | phone        | gender | type | birth               | is_delete |
+----+----------+--------------+--------+------+---------------------+-----------+
| 25 | Brody    | 0594-5398774 | 1      | 1    | 2016-12-11 20:12:36 | 0         |
| 24 | Kevyn    | 0594-5398240 | 0      | 2    | 2016-02-07 04:02:14 | 1         |
| 21 | Salvador | 0594-5399732 | 1      | 3    | 2016-01-01 15:01:21 | 0         |
| 22 | Brandon  | 0594-5396204 | 1      | 4    | 2016-05-12 06:05:40 | 1         |
| 18 | Robert   | 0594-5397830 | 0      | 5    | 2016-02-10 22:02:06 | 0         |
+----+----------+--------------+--------+------+---------------------+-----------+
5 rows in set (0.00 sec)

Mon Feb 22 18:41:11 2021
root@xxx(dba_metadata) > 

 

mysql5.7上执行:

[work@zzz scripts]$ mysql -hzzz -P3306  -uroot  -proot
Warning: Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 5048729
Server version: 5.7.25-28-log MySQL Community Server (GPL)

Copyright (c) 2009-2015 Percona LLC and/or its affiliates
Copyright (c) 2000, 2015, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

Mon Feb 22 18:41:18 2021
root@zzz ((none)) > use apple
Database changed
Mon Feb 22 18:41:21 2021
root@zzzj (apple) > select t2.* from (select t1.* from t_user t1 order by t1.id desc) as t2 group by t2.type;
+----+--------+--------------+--------+------+---------------------+-----------+
| id | name   | phone        | gender | type | birth               | is_delete |
+----+--------+--------------+--------+------+---------------------+-----------+
|  6 | Lee    | 0594-5394539 | 1      | 1    | 2015-10-23 08:10:23 | 1         |
| 14 | Miriam | 0594-5399081 | 0      | 2    | 2016-05-21 02:05:09 | 0         |
|  1 | James  | 0594-5397864 | 0      | 3    | 2016-01-30 19:01:09 | 1         |
|  2 | Hayes  | 0594-5392419 | 1      | 4    | 2015-12-24 11:12:27 | 1         |
|  3 | Diana  | 0594-5393520 | 1      | 5    | 2016-03-21 13:03:50 | 0         |
+----+--------+--------------+--------+------+---------------------+-----------+
5 rows in set (0.00 sec)

Mon Feb 22 18:41:29 2021

##############################################################################################################

解决办法:给排序后面加上limit:

root
@zzz (apple) > select t2.* from (select t1.* from t_user t1 order by t1.id desc limit 1000000) as t2 group by t2.type; +----+----------+--------------+--------+------+---------------------+-----------+ | id | name | phone | gender | type | birth | is_delete | +----+----------+--------------+--------+------+---------------------+-----------+ | 25 | Brody | 0594-5398774 | 1 | 1 | 2016-12-11 20:12:36 | 0 | | 24 | Kevyn | 0594-5398240 | 0 | 2 | 2016-02-07 04:02:14 | 1 | | 21 | Salvador | 0594-5399732 | 1 | 3 | 2016-01-01 15:01:21 | 0 | | 22 | Brandon | 0594-5396204 | 1 | 4 | 2016-05-12 06:05:40 | 1 | | 18 | Robert | 0594-5397830 | 0 | 5 | 2016-02-10 22:02:06 | 0 | +----+----------+--------------+--------+------+---------------------+-----------+ 5 rows in set (0.00 sec) Mon Feb 22 18:41:41 2021 root@zzz (apple) >

 

 原因:

          如果不加limit,系统会把order by优化掉,即mysql5.7中order by不起作用,

          在mysql5.7手册的8.2.2.1中有解释:

          子查询的优化是使用半连接的策略完成的(The optimizer uses semi-join strategies to improve subquery execution)使用半连接进行优化,子查询语句必须满足一些标准(In MySQL, a subquery must satisfy these criteria to be handled as a semi-join)。其中一个标准是:必须不是一个包含了limit和order by的语句(It must not have ORDER BY with LIMIT.)

 

 

二、sql_mode问题:

         MySQL 5.6中,其默认值为"NO_ENGINE_SU BSTITUTION",可理解为非严格模式,譬如,对自增主键插入空字符串'',虽然提示warning,但并不影响自增主键的生成。但在MySQL 5.7中,其就调整为了严格模式,对于上面这个,其不会提示warning,而是直接报错。

但是这个配置可以通过修改配置即可解决,我们线上升级前后都是为空,因此没啥问题。

        如果MySQL5.7中的sql_mode配置了ONLY_FULL_GROUP_BY,那么就要求select列表里只能出现分组列(即group by后面的列)和聚合函数(sum,avg,max等),这也是SQL92的标准。

 myql的group by语法为:

select 选取分组中的列 + 聚合函数 from 表名称 group by 分组的列

 

从语法格式来看,是现有分组,在确定检索的列,检索的列只能是参加分组了的列。

所以问题中,group by 后的a,b,c是先确定的,而select 后面的字段是可变的

正确的语句:

select a,b,c from table_name group by a,b,c,d;  
select a,b from table_name group by a,b,c;  
select a,max(a) from table_name group by a,b,c;  

 

以下是错误的:

select a,b,c from table_name group by a,b;  
select a,b,c from table_name group by a;  

 

 

三、max_allowed_packet问题:

 

 

 

 

##########################