【问题标题】:get bad data in group by and min mysql通过和 min mysql 在 group by 和 min mysql 中获取坏数据
【发布时间】:2020-02-23 00:34:27
【问题描述】:

我试图让年长的人加入团队,我使用 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

生日正确返回,但姓名和身份证不正确。

这里发生了什么?我的错误是什么?

【问题讨论】:

    标签: mysql sql datetime group-by


    【解决方案1】:

    这适用于每个 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');
    
    ✓ ✓
    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`
    
    编号 |姓名 |出生日期 -: | :-------- | :----------------- 1 |人1 | 1993-09-29 15:15:15 4 |人4 | 1990-09-29 15:15:15
    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');
    
    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`
    
    编号 |姓名 |出生日期 -: | :-------- | :----------------- 1 |人1 | 1993-09-29 15:15:15 5 |人5 | 1993-09-29 15:15:15 4 |人4 | 1990-09-29 15:15:15 6 |人6 | 1990-09-29 15:15:15

    db小提琴here

    【讨论】:

      【解决方案2】:

      这不是聚合查询。这是一个过滤查询。我建议在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;
      

      【讨论】:

        猜你喜欢
        • 2012-07-25
        • 2017-12-07
        • 1970-01-01
        • 2012-07-25
        • 2020-12-29
        • 2020-02-02
        • 2013-04-06
        • 2012-07-05
        • 2019-08-15
        相关资源
        最近更新 更多