![](/img/trans.png)
[英]mysql group by to return a the min value and get the corresponding row data
[英]get bad data in group by and min mysql
我试图让年长的人加入团队,我使用 group by 和 min 运算符,但它没有返回正确的值。
这是我的示例数据:
表人:
id | name | birthdate | team_id
-----------------------------------------
1 | person1 | 1993-09-29 15:15:15 | 1
2 | person2 | 1994-09-29 15:15:15 | 1
3 | person3 | 1992-09-29 15:15:15 | 2
4 | person4 | 1990-09-29 15:15:15 | 2
我使用此查询按团队获取老年人:
select id, name, min(birthdate) from persons group by team_id
我希望查询返回这个数据:
id | name | min(birthdate)
----------------------------
1 | person1 | 1993-09-29 15:15:15
4 | person4 | 1990-09-29 15:15:15
但它正在返回:
id | name | min(birthdate)
----------------------------
1 | person1 | 1993-09-29 15:15:15
3 | person3 | 1990-09-29 15:15:15
正确返回了生日,但名称和 ID 不正确。
这里发生了什么? 我的错误是什么?
这适用于每个 mysql 版本。
但是你没有指定如果两个人生日相同会发生什么。
第二个结果显示了我的查询会发生什么。
CREATE TABLE persons ( `id` INTEGER, `name` VARCHAR(7), `birthdate` datetime, `team_id` INTEGER ); INSERT INTO persons (`id`, `name`, `birthdate`, `team_id`) VALUES ('1', 'person1', '1993-09-29 15:15:15', '1'), ('2', 'person2', '1994-09-29 15:15:15', '1'), ('3', 'person3', '1992-09-29 15:15:15', '2'), ('4', 'person4', '1990-09-29 15:15:15', '2');
\n ✓\n\n ✓\n
SELECT p.`id`, p.`name`, p.`birthdate`# FROM persons p INNER JOIN (select team_id, min(birthdate) minbirth from persons group by team_id) mint ON p.birthdate = mint.minbirth AND p.team_id = mint.team_id ORDER By p.team_id,p.`id`
\n身份证 | 姓名 | 生日 \n -: | :------ | :------------------\n 1 | 个人1 | 1993-09-29 15:15:15\n 4 | 人4 | 1990-09-29 15:15:15\n
INSERT INTO persons (`id`, `name`, `birthdate`, `team_id`) VALUES ('5', 'person5', '1993-09-29 15:15:15', '1'), ('6', 'person6', '1990-09-29 15:15:15', '2');
\n ✓\n
SELECT p.`id`, p.`name`, p.`birthdate`# FROM persons p INNER JOIN (select team_id, min(birthdate) minbirth from persons group by team_id) mint ON p.birthdate = mint.minbirth AND p.team_id = mint.team_id ORDER By p.team_id,p.`id`
\n身份证 | 姓名 | 生日 \n -: | :------ | :------------------\n 1 | 个人1 | 1993-09-29 15:15:15\n 5 | 人5 | 1993-09-29 15:15:15\n 4 | 人4 | 1990-09-29 15:15:15\n 6 | 人6 | 1990-09-29 15:15:15\n
db<> 在这里小提琴
这不是聚合查询。 这是一个过滤查询。 我推荐在WHERE
子句中使用相关子查询:
select p.* -- you can select only the columns you want
from persons p
where p.birthdate = (select min(p2.birthdate)
from persons p2
where p2.team_id = p.team_id
);
另一种非常常见的方法是使用窗口函数:
select p.*
from (select p.*,
rank() over (partition by team_id order by birthdate) as seqnum
from persons p
) p
where seqnum = 1;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.