gpt4 book ai didi

mysql - 同样的条件,不同的结果?

转载 作者:可可西里 更新时间:2023-11-01 08:06:27 24 4
gpt4 key购买 nike

通过阅读 MySQL 文档,我无法解释 phpMyAdmin 中这两个查询之间的区别:

SELECT * FROM f_ean GROUP BY ean HAVING type = 'media'

--> 给我 57059 个结果

SELECT ean, type FROM f_ean GROUP BY ean HAVING type = 'media'

--> 给我 73201 个结果

仅仅显示不同的列,查询的结果数怎么可能不同?

最佳答案

如果您尝试过滤记录,您应该使用WHERE,而不是HAVINGHAVING 用于在分组和排序发生后应用过滤器。

无论如何,问题在于 MySQL 如何使用 GROUP BYGROUP BY 应该与聚合一起使用; MySQL 为方便起见扩展了功能。由于对列进行排序和分组的方式,您会收到不同的结果。

MySQL extends the use of GROUP BY so that the select list can refer to nonaggregated columns not named in the GROUP BY clause. This means that the preceding query is legal in MySQL. You can use this feature to get better performance by avoiding unnecessary column sorting and grouping. However, this is useful primarily when all values in each nonaggregated column not named in the GROUP BY are the same for each group.

参见 extensions to GROUP BY .

关于mysql - 同样的条件,不同的结果?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14285286/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com