gpt4 book ai didi

mysql - SQL选择一组的最大值

转载 作者:行者123 更新时间:2023-11-29 03:55:53 27 4
gpt4 key购买 nike

<分区>

初始表是这样的:

Fruit  | Item_ID | Production_line | Amount_produced | Production_date 
---------------------------------------------------------------
Apples | 652 | 1 | 24 | 2016-05-12
Pears | 455 | 4 | 54 | 2016-05-16
Pears | 455 | 2 | 26 | 2016-05-13
Apples | 652 | 6 | 65 | 2016-05-14
Apples | 652 | 3 | 24 | 2016-05-21
Pears | 455 | 7 | 54 | 2016-05-17
Pears | 455 | 5 | 26 | 2016-05-15
Apples | 652 | 8 | 65 | 2016-05-22

我希望看到的结果是最高级别的生产线(因为它们根据它们所在的级别从 1 开始编号)按 Item_ID 与所有其他列分组:

Fruit  | Item_ID | Production_line | Amount_produced | Production_date 
---------------------------------------------------------------
Pears | 455 | 7 | 54 | 2016-05-17
Apples | 652 | 8 | 65 | 2016-05-22

当我在查询末尾使用 SELECT 和 MAX(Production_line) 以及 GROUP BY Item_ID 来根据 Item_ID 对水果进行分组时,我没有得到正确的生产日期(不确定它是否随机抽取一个或什么)也不是正确的生产量。

我在这个表中没有主键。

我在 phpMyAdmin 和 MySQL 中工作。

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com