gpt4 book ai didi

mysql - 从表中删除重复行后,从查询结果中返回唯一列表

转载 作者:行者123 更新时间:2023-11-29 10:21:29 25 4
gpt4 key购买 nike

我有两列product_idr_store_id,其中有几行具有相同的值。其余列行具有不同的值我有具有相同 r_store_idproduct_id 的重复行,因为每次我都必须向该表中添加新条目。我想要包含最新 update_dt 的唯一行列表(引用下面的数据库表)。

id | m_store_id |r_store_id|product_id | amount |update_dt |
1 | 4 | 1 | 45 | 10 |18/03/5 |
2 | 4 | 1 | 45 | 100 |18/03/9 |
3 | 4 | 1 | 45 | 20 |18/03/4 |
4 | 5 | 2 | 49 | 10 |18/03/8 |
5 | 5 | 2 | 49 | 60 |18/03/2 |
6 | 9 | 3 | 45 | 19 |18/03/5 |
7 | 9 | 3 | 45 | 56 |18/03/3 |

我的结果应该是这样的:

id | m_store_id |r_store_id|product_id | amount |update_dt |
2 | 7 | 1 | 45 | 100 |18/03/9 |
4 | 5 | 2 | 49 | 10 |18/03/8 |
6 | 9 | 3 | 45 | 19 |18/03/5 |

我想将此结果放入这样的列表中:

List<Sales> salesList = (List<Sales>) query.list();

我找不到简单的解决方案。请帮我解决这个问题!

最佳答案

我们可以为每个商店选择按时间顺序最新的更新,然后加入以获取所有变量:

select a.*
from mytable a
join (select m_store_id, r_store_id, product_id, max(update_dt) as maxdate
from mytable
group by 1,2,3) b
on a.m_store_id=b.m_store_id
and a.r_store_id=b.r_store_id
and a.product_id=b.product_id
and a.update_dt = b.maxdate;

关于mysql - 从表中删除重复行后,从查询结果中返回唯一列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49180570/

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