mySQL之group By的简单分析
2017-01-16 15:45
155 查看
1.测试数据:
很明显,我们会用到group by,
eg:
当然,我们也可以这么写:
![](https://s5.51cto.com/wyfs02/M02/8C/E2/wKiom1h8eISx61U2AADIvLAXf4Q632.jpg)
![](https://s5.51cto.com/wyfs02/M02/8C/DE/wKioL1h8eIXjW-mUAACyDDG__qk684.jpg)
所以,我们可以这么去写:
可以读一下这篇文字,可以更好的理解group by:http://www.cnblogs.com/wiseblog/articles/4475936.html
http://blog.csdn.net/john_hongming/article/details/42742965
http://www.th7.cn/db/mysql/201502/91713.shtml
http://yueliangdao0608.blog.51cto.com/397025/81278
说明:
(1) 在SQL标准中,作为from后的子表或者子查询,其中的order by 无效,会被忽略,这一点在mysql5.7/mariadb 10中得到修正,之前的版本并不严格:
![](https://s4.51cto.com/wyfs02/M01/8C/EC/wKiom1h9wz_zC7mrAACsBkBOsT4418.png)
(2) group by对数据分组后,除被group by 的column之外,会选择任意一条数据返回,也就是说,被group by的其他列是无序的。
(3) 经过测试,发现在mysql 5.7和mariadb 10.*版本中,对相同的group by column,取值是根据数据写入的顺序来排序,即id顺序;
(4) 在group by 之后加order by子句对返回值无效,因只order by只对返回集合排序;
(5) 参考资料
![](https://s5.51cto.com/wyfs02/M02/8C/E8/wKioL1h9xEqzzARpAACws04AkYc091.png)
![](https://s3.51cto.com/wyfs02/M00/8C/EC/wKiom1h9xEuC1gzzAACcrY2g444342.png)
Mariadb 10 :
![](https://s5.51cto.com/wyfs02/M00/8C/E8/wKioL1h9xGizWwFIAAC9gn4bypk324.png)
![](https://s2.51cto.com/wyfs02/M02/8C/E9/wKioL1h9xOyAzxevAAAU6rhTmmE008.png)
![](https://s3.51cto.com/wyfs02/M00/8C/EC/wKiom1h9xRXSvfIQAAAXp1CR8bk244.png)
测试1:
Mariadb10:
![](https://s1.51cto.com/wyfs02/M01/8C/E9/wKioL1h9xW3QCAkgAAALUkaftUM546.png)
结果:.取值是按照id从小到大的顺序来的,对相同的no值,按写入的物理排序进行的;
测试2:Percona5.5
测试3:Percona5.1
测试4:MySQL5.6,关闭sql_mode=ONLY_FULL_GROUP_BY
测试5:MySQL5.1,
(5.1的测试结果)
![](https://s2.51cto.com/wyfs02/M02/8C/EC/wKiom1h9xk7ifErRAAALUkaftUM430.png)
结果:
1.对name相同的值,按照no从大到小的顺序排列,对no相同的值,按物理排序从小到大取值;
测试6:mysql5.7,关闭sql_mode=ONLY_FULL_GROUP_BY
![](https://s5.51cto.com/wyfs02/M01/8C/E9/wKioL1h9xrrwYDQdAAAK-qms_F4922.png)
结果:取值是按照id从小到大的顺序来的,对相同的no值,按写入的物理排序进行的;
测试7:mariadb 10,对no加desc排序
alter tabletestorder add index idx_no_name(no desc, name);
select* from (select id,no,name from testorder order by no desc)a group by a.name;
![](https://s5.51cto.com/wyfs02/M00/8C/EA/wKioL1h96nPxrfbOAAAZMrgOYBU658.jpg)
结果:desc是按照写入顺序也就是索引顺序排序,不是按照大小排序
create table `t_class` ( `id` bigint (11), `name` varchar (150), `age` int (11), `class` int (11) ); insert into `t_class` (`id`, `name`, `age`, `class`) values('1','tom1','22','1'); insert into `t_class` (`id`, `name`, `age`, `class`) values('2','tom2','32','1'); insert into `t_class` (`id`, `name`, `age`, `class`) values('4','jerry2','33','2'); insert into `t_class` (`id`, `name`, `age`, `class`) values('5','hanchao','11','3'); insert into `t_class` (`id`, `name`, `age`, `class`) values('6','hanchao2','30','3'); insert into `t_class` (`id`, `name`, `age`, `class`) values('7','jerry3','32','2'); insert into `t_class` (`id`, `name`, `age`, `class`) values('8','jerry4','23','2'); insert into `t_class` (`id`, `name`, `age`, `class`) values('9','jerry1','33','2');2.需求:查找每个班年龄最大的学生的信息。
很明显,我们会用到group by,
eg:
SELECT id,NAME,MAX(age),class FROM t_class GROUP BY class;【错误的写法!!】但是,这是错误的写法!!
当然,我们也可以这么写:
SELECT * FROM ( SELECT * FROM t_class ORDER BY age DESC) AS b GROUP BY class;但是,我不建议这么写,因为这么写有些不可预知的事情发生。我们先order by 再group by ,结果可能不是取的年龄最大的学生信息,具体原因,我还在寻找中,当然你知道原因也可以告诉我,证据如下:
![](https://s5.51cto.com/wyfs02/M02/8C/E2/wKiom1h8eISx61U2AADIvLAXf4Q632.jpg)
![](https://s5.51cto.com/wyfs02/M02/8C/DE/wKioL1h8eIXjW-mUAACyDDG__qk684.jpg)
所以,我们可以这么去写:
/**方法1:**/ SELECT * FROM t_class t WHERE t.age = (SELECT MAX(age) FROM t_class WHERE t.class = class) ORDER BY class; /**方法2:**/ SELECT t_class.id,t_class.name,t_class.age,t_class.class FROM t_class INNER JOIN (SELECT class,MAX(age) AS maxAge FROM t_class GROUP BY class)t ON (t.class = t_class.class AND t_class.age = t.maxAge);至于那个方法的效率高,你可以自己测试一下!
可以读一下这篇文字,可以更好的理解group by:http://www.cnblogs.com/wiseblog/articles/4475936.html
http://blog.csdn.net/john_hongming/article/details/42742965
http://www.th7.cn/db/mysql/201502/91713.shtml
http://yueliangdao0608.blog.51cto.com/397025/81278
说明:
(1) 在SQL标准中,作为from后的子表或者子查询,其中的order by 无效,会被忽略,这一点在mysql5.7/mariadb 10中得到修正,之前的版本并不严格:
![](https://s4.51cto.com/wyfs02/M01/8C/EC/wKiom1h9wz_zC7mrAACsBkBOsT4418.png)
(2) group by对数据分组后,除被group by 的column之外,会选择任意一条数据返回,也就是说,被group by的其他列是无序的。
(3) 经过测试,发现在mysql 5.7和mariadb 10.*版本中,对相同的group by column,取值是根据数据写入的顺序来排序,即id顺序;
(4) 在group by 之后加order by子句对返回值无效,因只order by只对返回集合排序;
(5) 参考资料
![](https://s5.51cto.com/wyfs02/M02/8C/E8/wKioL1h9xEqzzARpAACws04AkYc091.png)
![](https://s3.51cto.com/wyfs02/M00/8C/EC/wKiom1h9xEuC1gzzAACcrY2g444342.png)
Mariadb 10 :
![](https://s5.51cto.com/wyfs02/M00/8C/E8/wKioL1h9xGizWwFIAAC9gn4bypk324.png)
测试内容: set namesutf8; createtable testorder(id int not null,no int not null,name char(10) not null,primary key(id))engine=innodb; insertinto testorder values (1,1,'Mike'),(2,2,'John'),(3,3,'wyett'),(4,4,'Herry'),(5,5,'Mike'),(6,1,'John'),(7,2,'John'),(8,1,'Mike'),(9,1,'Mike'); select* from testorder;
![](https://s2.51cto.com/wyfs02/M02/8C/E9/wKioL1h9xOyAzxevAAAU6rhTmmE008.png)
select* from testorder order by no desc;
![](https://s3.51cto.com/wyfs02/M00/8C/EC/wKiom1h9xRXSvfIQAAAXp1CR8bk244.png)
select* from (select id,no,name from testorder order by no desc)a group by a.name;group by的默认排序是asc,因此name列是按照首字母升序排序的,对name相同的值
测试1:
Mariadb10:
![](https://s1.51cto.com/wyfs02/M01/8C/E9/wKioL1h9xW3QCAkgAAALUkaftUM546.png)
结果:.取值是按照id从小到大的顺序来的,对相同的no值,按写入的物理排序进行的;
测试2:Percona5.5
测试3:Percona5.1
测试4:MySQL5.6,关闭sql_mode=ONLY_FULL_GROUP_BY
测试5:MySQL5.1,
(5.1的测试结果)
![](https://s2.51cto.com/wyfs02/M02/8C/EC/wKiom1h9xk7ifErRAAALUkaftUM430.png)
结果:
1.对name相同的值,按照no从大到小的顺序排列,对no相同的值,按物理排序从小到大取值;
测试6:mysql5.7,关闭sql_mode=ONLY_FULL_GROUP_BY
![](https://s5.51cto.com/wyfs02/M01/8C/E9/wKioL1h9xrrwYDQdAAAK-qms_F4922.png)
结果:取值是按照id从小到大的顺序来的,对相同的no值,按写入的物理排序进行的;
测试7:mariadb 10,对no加desc排序
alter tabletestorder add index idx_no_name(no desc, name);
select* from (select id,no,name from testorder order by no desc)a group by a.name;
![](https://s5.51cto.com/wyfs02/M00/8C/EA/wKioL1h96nPxrfbOAAAZMrgOYBU658.jpg)
结果:desc是按照写入顺序也就是索引顺序排序,不是按照大小排序
相关文章推荐
- MySQL简单查询性能分析
- Mysql中order by、group by、having的区别深入分析
- ODBC + WIN32 API 访问MYSQL 数据库实现简单QQ用户注册和登录 的代码分析
- 股票数据导出分析(一)---数据导入MySQL以及网页表格简单show出来
- MySQL修复表的简单分析
- 对Jbpm数据库应用的简单分析和在Mysql上实现的demo
- Mysql报错注入原理分析(count()、rand()、group by)
- MySQL协议简单分析
- 简单介绍MySQL中GROUP BY子句的使用
- 深度分析mysql GROUP BY 与 ORDER BY
- Mysql中order by、group by、having的区别深入分析
- 深度分析mysql GROUP BY 与 ORDER BY
- 深度分析mysql GROUP BY 与 ORDER BY
- mysql分表场景分析与简单分表操作
- 简单分析MySQL中的primary key功能
- mysql出现unauthenticated user简单分析以及解决方法
- 简单总结一下mysql的性能分析方法,陆续更新
- mysql慢日志查询and简单分析
- 深度分析mysql GROUP BY 与 ORDER BY