Das Problem der Gruppierung nach in MySQL. .
某草草
某草草 2017-05-18 10:52:23
0
9
694

In MySQL ist die folgende Schreibmethode zulässig. Aber ist es streng oder nicht? Unterstützt es ein solches Schreiben? Wenn Sie Zweifel haben, wie Sie dieses Problem lösen können, helfen Sie mir bitte.

select * from user group by user_name;
某草草
某草草

Antworte allen(9)
刘奇

最详细的文档说明在官网找到了。

MySQL 5.7.5 and up implements detection of functional dependence. If the ONLY_FULL_GROUP_BY SQL mode is enabled (which it is by default), MySQL rejects queries for which the select list, HAVING condition, or ORDER BY list refer to nonaggregated columns that are neither named in the GROUP BY clause nor are functionally dependent on them. (Before 5.7.5, MySQL does not detect functional dependency and ONLY_FULL_GROUP_BY is not enabled by default. For a description of pre-5.7.5 behavior, see the MySQL 5.6 Reference Manual.)

来源:https://dev.mysql.com/doc/ref...

给我你的怀抱

select中的字段需要在group by中强制写出来
select user_name from user group by user_name;

http://blog.csdn.net/u2830560...

刘奇

首先理解下分组是个什么概念,分组后可以达到什么样的效果;
分组是为了按组这个属性进行统计分析;
例如一个学生表中,按性别分组,可以统计出男生多少人,女生多少人。
在查询结果列中,必然是sum,count等聚合函数组成。例如:
select count(*),sex from student group by sex;
如果是select * from user group by user_name;是想要得到一个什么样的统计结果呢?
mysql5.6默认可以使用这种写法select * from user group by user_name,但实际上在内部对语句做过转换;
mysql5.7以后默认不能使用此中写法,会报错。
所以写之前先想想我需要通过分组统计什么内容,使用适合的聚合函数

Peter_Zhu

在SQL语句中使用GROUP BY要注意三点
1:不能使用别名;
2:除了函数字段,select中出现的字段都必须出现在group by中,
3:别名不能使用保留字
这三点MYSQL都是没有要求的!
我们再来看你这个语句,如果user表只有一个字段user_name ,那么这个语句没有问题,
如果user表有超过一个字段,那么这个语句在mysql是没有问题的,但是在oracle和sqlserver是有问题的

刘奇
select * from user group by user_name;
//这么写其实也没问题 但是 实际上 你使用 group by 的时候 你需要用的 就只有 user_name 这个字段吧(通常来说)
//用什么字段就取什么字段就好。不一定要用 ‘*’ 用谁取谁就好
伊谢尔伦

如果存在user_name这个字段,那么就没有问题。group by是按字段分组,通常和聚合函数一起使用,像你这个sql也是可以执行的。只不过user_name通常都是唯一的,按唯一字段去分组是没有意义的。

黄舟

这个属于mysql的特殊功能支持,如@xuexiphpa所说,可以通过参数关掉。

但不建议使用,group by分组后,理论上返回的记录数比分组前少了,一般会通过聚合函数来返回一些统计数据。
直接使用select *,就不确认返回的是那一条记录了。

滿天的星座

一般个聚合函数结合用的比较多

给我你的怀抱

group by聚合分组后,select子句中的元素最好只保持:
1、常数
2、group by指定的列名
3、聚合函数,如count()、avg()、sum(*)等等

你这样*的结果,只列出了每一个分组的一条记录,而且不知道是第一个还是是随机的一个值

Beliebte Tutorials
Mehr>
Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage
Über uns Haftungsausschluss Sitemap
Chinesische PHP-Website:Online-PHP-Schulung für das Gemeinwohl,Helfen Sie PHP-Lernenden, sich schnell weiterzuentwickeln!