gpt4 book ai didi

mysql - 从同一行选择最新的日期时间列和相关价格列

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

我的数据库中有一个表正在跟踪一些价格。这些列是:id产品工厂卖家平台价格插入(日期时间)。

每天每 6 小时就会存储当前价格的新记录。示例:

1, Product 1, Factory 1, Seller 1, Platform 1, 99.99, 2019-08-07 02:12:35
2, Product 1, Factory 1, Seller 1, Platform 1, 95.99, 2019-08-06 20:12:35

现在我尝试选择带有价格的最新条目。一个条目对于产品工厂卖家平台应该是唯一的。所以我的 sql 查询现在是:

SELECT * 
FROM prices
GROUP
BY product
, factory
, seller
, platform
ORDER
BY inserted DESC
, price ASC

但是我现在如何获取每个条目的最新价格?一种解决方案是选择 MAX(`inserted`),但如何从 MAX(`inserted`) 行获取价格?

最佳答案

select p1.* from 
prices p1
join
(
SELECT `product`, `factory`, `seller`, `platform`, max(inserted) as max_inserted
FROM `prices`
GROUP BY `product`, `factory`, `seller`, `platform`
) p2 on p1.product = p2.product
and p1.factory = p2.factory
and p1.seller = p2.seller
and p1.platform = p2.platform
and p1.inserted = p2.max_inserted

内部选择获取每个组的最新日期。然后加入该记录以仅获取包含价格的相关记录。

关于mysql - 从同一行选择最新的日期时间列和相关价格列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57387342/

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