
| 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'); 1 wuxi889 OP 使用分组在临时表中查询会有这种情况,但是把临时表存入一个实表中就是可以正常使用 |
2 mitsuizzz 2021-02-23 14:27:48 +08:00 https://imgchr.com/i/yqU9rd 用的你的插入和查询 sql 和你查出来的数据不一样,我是 mysql5.7 |
5 Rache1 2021-02-24 09:23:29 +08:00 group_concat 最好单独套一层用,不然会有意想不到的结果 |
6 Rocketer 2021-02-24 10:03:26 +08:00 via iPhone 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 每一步的输出就是下一步的输入,自己推演一下试试吧 |
7 lyz0205 2021-02-24 11:04:22 +08:00 mysql 8.0 没能重现你说的问题,你的 mysql 是哪个版本? |