数据
id amount created_at
"1" "105" "2018-01-28 15:22:00"
"2" "120" "2018-01-28 15:23:00"
"3" "200" "2018-01-28 15:24:00"
"4" "205" "2018-01-28 15:24:10"
"5" "230" "2018-01-28 15:25:00"
这是我的表字段和数据,我的cronjob每分钟运行一次,但由于某种原因,它可能会在同一分钟再次运行,因此我需要按日期到分钟过滤数据组
我尝试了什么
SELECT COUNT(*), SUM(`amount`), MAX(`amount`), MIN(`amount`)
FROM (
SELECT *
FROM `stats`
GROUP BY DATE_FORMAT(`created_at`, '%Y-%m-%d %H:%i')
) AS tmp
GROUP BY DATE_FORMAT(`created_at`, '%Y-%m-%d %H:%i')
结果正是我所需要的,但我听说子查询始终是最后的选择,因为效率问题,还有另一种方法吗?
"1" "105" "105" "105"
"1" "120" "120" "120"
"1" "200" "200" "200"
"1" "230" "230" "230"
答案 0 :(得分:0)
您的预期输出意味着您希望每分钟有一条记录,以及该分钟内id或金额最小的记录。假设您想要最小的id,那么您可以尝试以下查询:
SELECT s1.*
FROM stats s1
INNER JOIN
(
SELECT
DATE_FORMAT(created_at, '%Y-%m-%d %H:%i') AS created_at
MIN(id) AS min_id
FROM stats
GROUP BY DATE_FORMAT(created_at, '%Y-%m-%d %H:%i')
) s2
ON DATE_FORMAT(s1.created_at, '%Y-%m-%d %H:%i') = s2.created_at AND
s1.amount = s2.min_id;