删除 GROUP BY 后 MySQL 查询返回的结果更少

发布于 2024-12-01 14:45:20 字数 1743 浏览 1 评论 0原文

我在包含 GROUP BY 的查询时遇到问题。作为诊断的一部分,我删除了 GROUP BY 语句以便查看原始数据 - 然而,当我这样做时,我得到的行数少于我使用 GROUP BY 所做的行数。

重现:

CREATE TABLE `test1` (
  `date` bigint(20) DEFAULT NULL,
  `quantity` int(11) DEFAULT NULL,
  `processed` int(11) DEFAULT NULL
) ENGINE=MyISAM DEFAULT CHARSET=latin1
INSERT INTO `test1` VALUES (1312483084,3,2),(1312483084,1,2),(1312483148,1,2),(1312483148,1,2),(1314038654,1,2),(1314301805,1,2);

现在运行我原来的 SELECT(使用 GROUP BY 完成):

SELECT DATEDIFF(from_unixtime(test1.date), from_unixtime(1311801510)) DIV 28 AS date_idx,
min(DATEDIFF(from_unixtime(test1.date), from_unixtime(1311801510))) AS orig_date_idx,            SUM(test1.quantity) AS num_items,
IF(test1.processed in (2,3,4), 'Complete','Pending') as status
FROM test1
GROUP BY status, date_idx \G

您应该返回两行:

*************************** 1. row ***************************
     date_idx: 0
orig_date_idx: 8
    num_items: 7
       status: Complete
*************************** 2. row ***************************
     date_idx: 1
orig_date_idx: 29
    num_items: 1
       status: Complete

现在删除 GROUP BY,即:

SELECT DATEDIFF(from_unixtime(test1.date), from_unixtime(1311801510)) DIV 28 AS date_idx,
min(DATEDIFF(from_unixtime(test1.date), from_unixtime(1311801510))) AS orig_date_idx,            SUM(test1.quantity) AS num_items,
IF(test1.processed in (2,3,4), 'Complete','Pending') as status
FROM test1

突然间您只返回一行 - 其中 date_idx == 1 的行原始结果已合并到 date_idx == 0 的条目中

*************************** 1. row ***************************
     date_idx: 0
orig_date_idx: 8
    num_items: 8
       status: Complete

非常感谢所有想法!

I'm having a problem with a query containing a GROUP BY. As part of diagnosing it I removed the GROUP BY statement in order to see the raw data - however when I do that I get FEWER rows returned that I do with the GROUP BY.

To reproduce:

CREATE TABLE `test1` (
  `date` bigint(20) DEFAULT NULL,
  `quantity` int(11) DEFAULT NULL,
  `processed` int(11) DEFAULT NULL
) ENGINE=MyISAM DEFAULT CHARSET=latin1
INSERT INTO `test1` VALUES (1312483084,3,2),(1312483084,1,2),(1312483148,1,2),(1312483148,1,2),(1314038654,1,2),(1314301805,1,2);

Now run my original SELECT (Complete with GROUP BY):

SELECT DATEDIFF(from_unixtime(test1.date), from_unixtime(1311801510)) DIV 28 AS date_idx,
min(DATEDIFF(from_unixtime(test1.date), from_unixtime(1311801510))) AS orig_date_idx,            SUM(test1.quantity) AS num_items,
IF(test1.processed in (2,3,4), 'Complete','Pending') as status
FROM test1
GROUP BY status, date_idx \G

You should get two rows returned:

*************************** 1. row ***************************
     date_idx: 0
orig_date_idx: 8
    num_items: 7
       status: Complete
*************************** 2. row ***************************
     date_idx: 1
orig_date_idx: 29
    num_items: 1
       status: Complete

Now remove the GROUP BY, ie:

SELECT DATEDIFF(from_unixtime(test1.date), from_unixtime(1311801510)) DIV 28 AS date_idx,
min(DATEDIFF(from_unixtime(test1.date), from_unixtime(1311801510))) AS orig_date_idx,            SUM(test1.quantity) AS num_items,
IF(test1.processed in (2,3,4), 'Complete','Pending') as status
FROM test1

All of a sudden you only get one row returned - the row that had date_idx == 1 in the original result has been merged into the entry for date_idx == 0

*************************** 1. row ***************************
     date_idx: 0
orig_date_idx: 8
    num_items: 8
       status: Complete

All ideas gratefully received!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

站稳脚跟 2024-12-08 14:45:20

坦白说,我很惊讶它运行时不会因为使用没有分组依据的聚合函数而给出错误,可能与所有字段都是计算值这一事实有关。无论如何,摆脱所有聚合函数来获取底层数据。

Frankly I'm surprised that it runs without just giving you an error for using aggregate functions without a group by, maybe has something to do with the fact that all your fields are calculated values. Anyway, get rid of all your aggregate functions to get to the underlying data.

甜警司 2024-12-08 14:45:20

事实上,您删除了 GROUP BY 指令,因此执行查询时不需要对 date_idx 上的结果进行细分。数字计数同样从 GROUPED 查询中的 1 和 7 到 NON GROUPED 查询中的 8。如果没有 GROUPBY 指令,查询执行器就没有理由为您提供多于一行的聚合信息,因为您根本没有要求进行这种分离。

The fact that you are removing the GROUP BY directive makes it unneeded for the execution of the query to subdivide your results on date_idx. The number count is equally going from 1 and 7 in the GROUPED query to 8 in the NON GROUPED query. Without the GROUPBY directive, there is no reason for the query executor to give you more than one row with aggregated information, because you simply did not ask for that seperation.

坠似风落 2024-12-08 14:45:20

总和是一个聚合,因此会为您折叠行。然而,在还使用 group by 子句的情况下,这是优先的,然后对不同的数据(来自分组)进行求和。在这种情况下,分组生成两个不同的集合,然后将它们相加。其他数据可能会通过 group by 返回不同的结果。

The sum is an aggregation so is collapsing the rows down for you. However in the case of also using the group by clause, that is taking precedence, with varying data (from the grouping) then being summed. In this case the group by generates two different collections which are then summed. Other data may return different results with the group by.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文