请教个 MySQL 中 group by 与 distinct 一同使用的的问题

2021-02-23 14:00:17 +08:00
 wuxi889
sid eid datetime
73753 4 2021-02-23 09:00
73753 7 2021-02-23 09:00
73753 2 2021-02-23 09:00
73757 2 2021-02-23 10:00
73756 2 2021-02-23 11:00
73756 2 2021-02-23 11:00
73756 2 2021-02-23 11:00
73756 7 2021-02-23 11:00
73756 4 2021-02-23 11:00
73756 2 2021-02-23 11:00
73760 7 2021-02-23 11:00
73760 4 2021-02-23 11:00
73759 7 2021-02-23 11:00
73759 4 2021-02-23 11:00
73759 2 2021-02-23 11:00
73758 4 2021-02-23 11:00
73758 2 2021-02-23 11:00

以上是自查询后的临时表数据,现在我要使用 group by 对 eid 与 datetime 进行分组

这个是查询语句:

SELECT
	`eid`,
	MIN( `datetime` ) AS `datetime`,
	COUNT(DISTINCT( `sid` )) AS `value`,
	GROUP_CONCAT(`sid`) AS `sids`
FROM
    table
GROUP BY
	`eid`, 
	`datetime`
ORDER BY
        `datetime`

得出对结果却是

eid datetime value sids
2 2021-02-23 09:00 1 73753
4 2021-02-23 09:00 1 73753
7 2021-02-23 09:00 1 73753
2 2021-02-23 10:00 1 73757
2 2021-02-23 11:00 1 73756
2 2021-02-23 11:00 3 73758,73756,73759,73756,73756
4 2021-02-23 11:00 4 73756,73760,73759,73758
7 2021-02-23 11:00 3 73760,73759,73756

这里出现了两个 eid = 2 && datetime = 2021-02-23 11:00 我试了一下,知道是 DISTINCT 导致的问题,但是没想明白其中的原因,希望大佬讲解一下。

INSERT INTO `temp_table`(`sid`, `eid`, `datetime`) VALUES (73753, 4, '2021-02-23 09:00');
INSERT INTO `temp_table`(`sid`, `eid`, `datetime`) VALUES (73753, 7, '2021-02-23 09:00');
INSERT INTO `temp_table`(`sid`, `eid`, `datetime`) VALUES (73753, 2, '2021-02-23 09:00');
INSERT INTO `temp_table`(`sid`, `eid`, `datetime`) VALUES (73757, 2, '2021-02-23 10:00');
INSERT INTO `temp_table`(`sid`, `eid`, `datetime`) VALUES (73756, 2, '2021-02-23 11:00');
INSERT INTO `temp_table`(`sid`, `eid`, `datetime`) VALUES (73756, 2, '2021-02-23 11:00');
INSERT INTO `temp_table`(`sid`, `eid`, `datetime`) VALUES (73756, 2, '2021-02-23 11:00');
INSERT INTO `temp_table`(`sid`, `eid`, `datetime`) VALUES (73756, 7, '2021-02-23 11:00');
INSERT INTO `temp_table`(`sid`, `eid`, `datetime`) VALUES (73756, 4, '2021-02-23 11:00');
INSERT INTO `temp_table`(`sid`, `eid`, `datetime`) VALUES (73756, 2, '2021-02-23 11:00');
INSERT INTO `temp_table`(`sid`, `eid`, `datetime`) VALUES (73760, 7, '2021-02-23 11:00');
INSERT INTO `temp_table`(`sid`, `eid`, `datetime`) VALUES (73760, 4, '2021-02-23 11:00');
INSERT INTO `temp_table`(`sid`, `eid`, `datetime`) VALUES (73759, 7, '2021-02-23 11:00');
INSERT INTO `temp_table`(`sid`, `eid`, `datetime`) VALUES (73759, 4, '2021-02-23 11:00');
INSERT INTO `temp_table`(`sid`, `eid`, `datetime`) VALUES (73759, 2, '2021-02-23 11:00');
INSERT INTO `temp_table`(`sid`, `eid`, `datetime`) VALUES (73758, 4, '2021-02-23 11:00');
INSERT INTO `temp_table`(`sid`, `eid`, `datetime`) VALUES (73758, 2, '2021-02-23 11:00');
1987 次点击
所在节点    MySQL
7 条回复
wuxi889
2021-02-23 14:27:26 +08:00
使用分组在临时表中查询会有这种情况,但是把临时表存入一个实表中就是可以正常使用
mitsuizzz
2021-02-23 14:27:48 +08:00
https://imgchr.com/i/yqU9rd 用的你的插入和查询 sql 和你查出来的数据不一样,我是 mysql5.7
wuxi889
2021-02-23 14:38:22 +08:00
@mitsuizzz 对,因为我使用对数据是临时表查询对结果再进行分组,但是把临时表存入一个实表中就是可以正常使用
mitsuizzz
2021-02-23 16:57:00 +08:00
@wuxi889 你是不是查临时表内部用了什么排序,我记得外部 group by 后内部排序会失效
Rache1
2021-02-24 09:23:29 +08:00
group_concat 最好单独套一层用,不然会有意想不到的结果 😂
Rocketer
2021-02-24 10:03:26 +08:00
SQL 命令真正的执行顺序如下:

(1) FROM
(2) JOIN
(3) ON
(4) WHERE
(5) GROUP BY
(6) WITH
(7) HAVING
(8) SELECT
(9) DISTINCT
(10) ORDER BY
(11) LIMIT

每一步的输出就是下一步的输入,自己推演一下试试吧
lyz0205
2021-02-24 11:04:22 +08:00
mysql 8.0 没能重现你说的问题,你的 mysql 是哪个版本?

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/755445

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX