我想使用分组(open
)的MIN(id)行中exchange, base_currency, quote_currency, DATE(created_at)
的值来更新组(last
)的MAX(id)的id last open exchange base_curr quote_curr created_at
6 1.11 0.00 ex1 usd yen 2018-07-29 03:00:00 --> update open with 1.14 (value of last from MIN(id) of group)
5 1.09 0.00 ex1 usd yen 2018-07-29 02:00:00
4 1.14 0.00 ex1 usd yen 2018-07-29 01:00:00
3 0.49 0.00 ex2 yen won 2018-07-29 03:00:00 --> update open with 0.50 (value of last from MIN(id) of group)
2 0.51 0.00 ex2 yen won 2018-07-29 02:00:00
1 0.50 0.00 ex2 yen won 2018-07-29 01:00:00
值同一组。
last
我了解如何获取组中的所有MIN(id),但不确定如何使用MIN(id)行中的open
值来更新{{ 1}}组的MAX(id)值。
MAX(id)或MAX(created_at)将为我提供组的最新行。
SELECT MIN(id) as min_id, last
FROM tickers
WHERE DATE(created_at) = '2018-07-29'
GROUP BY exchange, base_currency, quote_currency, DATE(created_at)
答案 0 :(得分:1)
您可以在子查询中计算最小值/最大值。然后使用另一个join
从最小值行中引入值:
update tickers t join
(select exchange, base_curr, quote_curr, date(created_at) as created_at_date,
max(id) as maxid, min(id) as minid
from tickers t2
group by exchange, base_curr, quote_curr, date(created_at)
) tt
on tt.maxid = t.id join
tickers tmin
on tmin.id = tt.minid
set t.open = tmin.last;