[英]SQL: How to select one record per day, assuming that each day contain more than 1 value MySQL
I want to select records from '2013-04-01 00:00:00'
to 'today'
but, each day has lot of value, because they are saving each 15 minutes a value, so I want only the first or last value from each day.我想 select 记录从
'2013-04-01 00:00:00'
到'today'
但是,每一天都有很多价值,因为它们每 15 分钟保存一个值,所以我只想要第一个或最后一个值从每一天开始。
Table schema:表架构:
CREATE TABLE IF NOT EXISTS `value_magnitudes` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`value` float DEFAULT NULL,
`magnitude_id` int(11) DEFAULT NULL,
`sdi_belongs_id` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
`reading_date` datetime DEFAULT NULL,
`created_at` datetime DEFAULT NULL,
`updated_at` datetime DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=1118402 ;
Bad SQL:坏 SQL:
SELECT value FROM `value_magnitudes` WHERE `value_magnitudes`.`reading_date` BETWEEN '2013-04-01 00:00:00' AND '2013-04-02 00:00:00' AND (`value_magnitudes`.magnitude_id = 234) LIMIT 1
SELECT value FROM `value_magnitudes` WHERE `value_magnitudes`.`reading_date` BETWEEN '2013-04-02 00:00:00' AND '2013-04-03 00:00:00' AND (`value_magnitudes`.magnitude_id = 234) LIMIT 1
SELECT value FROM `value_magnitudes` WHERE `value_magnitudes`.`reading_date` BETWEEN '2013-04-03 00:00:00' AND '2013-04-04 00:00:00' AND (`value_magnitudes`.magnitude_id = 234) LIMIT 1
SELECT value FROM `value_magnitudes` WHERE `value_magnitudes`.`reading_date` BETWEEN '2013-04-04 00:00:00' AND '2013-04-05 00:00:00' AND (`value_magnitudes`.magnitude_id = 234) LIMIT 1
SELECT value FROM `value_magnitudes` WHERE `value_magnitudes`.`reading_date` BETWEEN '2013-04-05 00:00:00' AND '2013-04-06 00:00:00' AND (`value_magnitudes`.magnitude_id = 234) LIMIT 1
etc ...
I want all in one if possible...如果可能的话,我想合一……
Thank you a lot.十分感谢。
EDIT: I mean, I have a query per day, but I just want to make a single query from reading_date >= '2013-04-01 00:00:00'
c编辑:我的意思是,我每天都有一个查询,但我只想从
reading_date >= '2013-04-01 00:00:00'
c 进行一个查询
EDIT2: I have 64,260 records in that table.value_magnitudes
and it takes sooooooooo long to excecute and response that query, and sometimes timeout conection. EDIT2:我在该
table.value_magnitudes
中有64,260条记录,执行和响应该查询需要很长时间,有时连接超时。
To get the first entry for every date you can do 获取您可以执行的每个日期的第一个条目
select * from value_magnitudes
where id in
(
SELECT min(id)
FROM value_magnitudes
WHERE magnitude_id = 234
and date(reading_date) >= '2013-04-01'
group by date(reading_date)
)
select * from value_magnitudes
where id in
(
SELECT min(id)
FROM value_magnitudes
WHERE `value_magnitudes`.`reading_date` BETWEEN '$from_selected' AND '$to_selected' and (magnitude_id = 234) group by date(reading_date)
)
using the above example - in case someone needs this - it's searching for invalid records where action should not be 0 if its the first row of the day:使用上面的例子 - 如果有人需要这个 - 它正在搜索无效记录,如果它是当天的第一行,则 action 不应为 0:
select * from log
where action=0 AND id in
(
SELECT min(id)
FROM log
group by date(ts)
)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.