我有数据库'价格'有100万行。
它由3列组成: 价钱 大体时间 时间戳
我想删除所有具有相同timeframe
和timestamp
的行,但要保留一行。
示例:
price timeframe timestamp
14 60 1508407200
1.6 60 1508407200
26 90 1508407200
运行查询后,我会得到:
price timeframe timestamp
14 60 1508407200
26 90 1508407200
你会用什么MySql查询来做到这一点?
答案 0 :(得分:1)
我可能会建议汇总
select max(price) as price, timeframe, timestamp
from prices
group by timeframe, timestamp;
如果您始终知道赠送timeframe
/ timestamp
组合的价格是唯一的,那么您可以这样做:
select p.*
from prices p
where p.price = (select max(p2.price) from prices p2 where p2.timeframe = p.timeframe and p2.timestamp = p.timestamp);
这样做的好处是您可以轻松地在同一行上获取其他列。