Mysql 15分钟后第一排
我要15分钟后的第一张唱片 我有一个MySQL表,它有大约1分钟间隔的datetime行,我想检索第一条记录,然后在接下来的15分钟内再次检索第一条记录。我不知道是使用join还是MySQL变量,还是使用嵌套查询。即使您不知道正确的指南,我应该使用变量、连接或嵌套什么 具体来说,我们有一个表,其中有一列date\u时间戳,如下所示Mysql 15分钟后第一排,mysql,sql,database,Mysql,Sql,Database,我要15分钟后的第一张唱片 我有一个MySQL表,它有大约1分钟间隔的datetime行,我想检索第一条记录,然后在接下来的15分钟内再次检索第一条记录。我不知道是使用join还是MySQL变量,还是使用嵌套查询。即使您不知道正确的指南,我应该使用变量、连接或嵌套什么 具体来说,我们有一个表,其中有一列date\u时间戳,如下所示 +---------------------+ | date_timestamp | +---------------------+ | 2014-01-0
+---------------------+
| date_timestamp |
+---------------------+
| 2014-01-07 11:49:42 |
| 2014-01-07 11:50:12 |
| 2014-01-07 11:50:31 |
| 2014-01-07 11:50:42 |
| 2014-01-07 11:51:22 |
| 2014-01-07 11:51:42 |
| 2014-01-07 11:52:52 |
| 2014-01-07 11:53:12 |
| 2014-01-07 11:53:32 |
| 2014-01-07 11:54:22 |
| 2014-01-07 11:55:42 |
| 2014-01-07 11:58:02 |
| 2014-01-07 11:59:22 |
| 2014-01-07 12:00:02 |
| 2014-01-07 12:00:42 |
| 2014-01-07 12:01:32 |
| 2014-01-07 12:01:52 |
| 2014-01-07 12:02:22 |
| 2014-01-07 12:03:42 |
| 2014-01-07 12:04:42 |
| 2014-01-07 12:05:02 |
+---------------------+
i want the output to be if i select 5 minute interval
+---------------------+
| date_timestamp |
+---------------------+
| 2014-01-07 11:49:42 |
| 2014-01-07 11:54:22 |
| 2014-01-07 11:59:22 |
| 2014-01-07 12:04:42 |
+---------------------+
问题不清楚,, 但如果您使用的是mYSQL,请获取最后插入的行
SELECT *
FROM tablename
ORDER BY id DESC
LIMIT 1
或
更新(调整以满足您的样本数据并忽略秒数):
select date_timestamp from (
select
date_timestamp
, if(date_format(date_timestamp, '%Y-%m-%d %H:%i:00') = @a, 1, 0) as equals
, if(date_format(date_timestamp, '%Y-%m-%d %H:%i:00') = @a, @a:=@a + interval 5 minute, @a) as mya
from my_table,
(select @a := (select date_format(min(date_timestamp), '%Y-%m-%d %H:%i:00') from my_table)) var_init
order by date_timestamp
) sq
where equals = 1;
- 现场观看它的工作
select date_timestamp from (
select
date_timestamp
, if(date_format(date_timestamp, '%Y-%m-%d %H:%i:00') = @a, 1, 0) as equals
, if(date_format(date_timestamp, '%Y-%m-%d %H:%i:00') = @a, @a:=@a + interval 5 minute, @a) as mya
from my_table,
(select @a := (select date_format(min(date_timestamp), '%Y-%m-%d %H:%i:00') from my_table)) var_init
order by date_timestamp
) sq
where equals = 1;
我会使用用户定义的变量
测试数据:
create table interv(id int auto_increment primary key, myd datetime);
insert into interv(myd) values
(now()),
(now() + interval 7 minute),
(now() + interval 9 minute),
(now() + interval 15 minute),
(now() + interval 16 minute),
(now() + interval 30 minute),
(now() + interval 35 minute);
查询:
select id, myd from (
select
id
, myd
, if(myd = @a, 1, 0) as equals
, if(myd = @a, @a:=@a + interval 15 minute, @a) as mya
from interv,
(select @a := (select min(myd) from interv)) var_init
order by myd
) sq
where equals = 1;
结果:
| ID | MYD |
|----|------------------------------|
| 1 | March, 28 2014 09:03:23+0000 |
| 4 | March, 28 2014 09:18:23+0000 |
| 6 | March, 28 2014 09:33:23+0000 |
- 现场观看它的工作