MYSQL从历史记录表中获取两个不同日期的产品记录,如果发现重复数据,则只包含最新数据
我有一个交易表,其中包含不同类型的产品,其中包含两种类型的产品 存储在标记为product_type1和product_type2的others字段(json字段)中,以及 产品可以标记为两者 交易表:MYSQL从历史记录表中获取两个不同日期的产品记录,如果发现重复数据,则只包含最新数据,mysql,Mysql,我有一个交易表,其中包含不同类型的产品,其中包含两种类型的产品 存储在标记为product_type1和product_type2的others字段(json字段)中,以及 产品可以标记为两者 交易表: id, hash_value, product_name,price, date , others 1, abc, product_name1, 20, 2018-10-21, {"product_type1": 1} 2, def, product_name2, 30, 2018-10-22
id, hash_value, product_name,price, date , others
1, abc, product_name1, 20, 2018-10-21, {"product_type1": 1}
2, def, product_name2, 30, 2018-10-22, {"product_type2": 1}
3, ghi, product_name3, 40, 2018-10-21, {"product_type1": 1, "product_type2": 1}
唯一键(id、哈希值)
对于上面提到的表,我们有一个存储历史产品的历史表
对于事务表中的上述产品(模式与事务表相同,但唯一键位于id、哈希值、日期上)
历史记录表
id, hash_value, product_name, price, date , others
1, abc, product_name1, 20, 2018-10-21, {"product_type1": 1}
2, abc, product_name1, 18, 2018-10-20, {"product_type1": 1}
3, abc, product_name1, 19, 2018-10-19, {"product_type1": 1}
4, def, product_name2, 30, 2018-10-22, {"product_type2": 1}
4, def, product_name2, 29, 2018-10-21, {"product_type2": 1}
4, def, product_name2, 40, 2018-10-20, {"product_type2": 1}
5, ghi, product_name3, 40, 2018-10-21, {"product_type1": 1, "product_type2": 1}
5, ghi, product_name3, 50, 2018-10-22, {"product_type1": 1, "product_type2": 1}
我想查询具有以下条件的记录。
1.获取2018年10月21日标记为product_type1的所有记录
2.获取2018年10月22日标记为产品类型2的所有记录
3.如果标记为两者,则获取具有最新日期的记录
我试过的问题
SELECT *
FROM ((SELECT *
FROM history_table
WHERE date = '2018-10-21'
AND others ->> '$.product_type1' == 1)
UNION ALL
(SELECT *
FROM history_table
WHERE date = '2018-10-22'
AND others ->> '$.product_type2' == 1))
但我得到的结果是
id, hash_value, product_name, price, date , others
1, abc, product_name1, 20, 2018-10-21, {"product_type1": 1}
4, def, product_name2, 30, 2018-10-22, {"product_type2": 1}
5, ghi, product_name3, 40, 2018-10-21, {"product_type1": 1, "product_type2": 1}
5, ghi, product_name3, 50, 2018-10-22, {"product_type1": 1, "product_type2": 1}
如果存在id和哈希值的重复记录,则结果应仅给出具有最新日期的记录
预期结果:
id, hash_value, product_name, price, date , others
1, abc, product_name1, 20, 2018-10-21, {"product_type1": 1}
4, def, product_name2, 30, 2018-10-22, {"product_type2": 1}
5, ghi, product_name3, 50, 2018-10-22, {"product_type1": 1, "product_type2": 1}
- 在(子选择查询)中,确定符合规定条件的
和id
hash_值组合的最大日期值
- 现在,只需将此结果集连接到
,即可获得仅与最大日期值对应的行history\u表
- 而不是使用
组合两个不同的条件选择结果;您可以在此处的Union All
条件中直接使用Where
或
。这将大大提高效率
SELECT ht.* FROM
history_table AS ht
JOIN
(
SELECT id, hash_value, MAX(date) AS max_date
FROM history_table
WHERE (date = '2018-10-21' AND others ->> '$.product_type1' == 1)
OR
(date = '2018-10-22' AND others ->> '$.product_type2' == 1)
GROUP BY id, hash_value
) AS dt ON dt.id = ht.id AND
dt.hash_value = ht.hash_value AND
dt.max_date = ht.date
您的MySQL服务器版本是什么?是否为8.0.2及更高版本?mysql版本14.14发行版5.7.24这看起来不错,但在我的表中有一个日期时间字段,我需要在该字段上使用max(日期时间),因为对于特定的一天,我可能有许多记录,我的日期时间值中很少存储为0000-00-00:00:00(YYYY-MM-DD hh:MM:ss)可能是因为我的代码中有错误。max将这些datetime作为max。如果同一日期已经有另一个datetime值,@rohitwernekar max将不会返回0000。您能准备一个SQL FIDLE或DB FIDLE来显示您的错误吗?很抱歉,您的查询中没有错误,查询非常完美,谢谢:)