Mysql 从最接近选定日期的数据库中获取唯一数据
我想得到最接近某个特定日期的数据,无论是在过去还是未来,以更接近该日期的为准。但我也需要它的类型不同。例如:Mysql 从最接近选定日期的数据库中获取唯一数据,mysql,sql,Mysql,Sql,我想得到最接近某个特定日期的数据,无论是在过去还是未来,以更接近该日期的为准。但我也需要它的类型不同。例如: id |type |data | date 1 |0 |1904 |2018-08-19 00:14:32 2 |0 |1904 |2018-08-19 00:24:47 3 |0 |1904 |2018-08-19 05:02:30 4 |0 |1904 |2018-08-22 00:05:58 5 |0 |190
id |type |data | date
1 |0 |1904 |2018-08-19 00:14:32
2 |0 |1904 |2018-08-19 00:24:47
3 |0 |1904 |2018-08-19 05:02:30
4 |0 |1904 |2018-08-22 00:05:58
5 |0 |1904 |2018-08-22 00:08:34
6 |4 |1903 |2018-08-19 00:14:31
7 |6 |1926 |2018-08-19 00:14:32
8 |6 |1926 |2018-08-19 04:44:10
9 |6 |1926 |2018-08-19 04:52:58
10 |6 |1926 |2018-08-19 04:59:23
11 |6 |1926 |2018-08-22 00:05:58
12 |6 |1926 |2018-08-22 00:07:52
13 |6 |1926 |2018-08-22 00:08:34
14 |7 |1564 |2018-08-19 00:14:32
15 |8 |1900 |2018-08-19 00:14:32
如果我指定日期时间:2018-08-19 00:00:00。我应该得到以下结果:
id | type
1 | 0
6 | 4
7 | 6
14 | 7
15 | 8
或者如果我指定日期时间:2018-09-03 00:00:00。我期望:
id | type
5 | 0
6 | 4
13 | 6
14 | 7
15 | 8
或者最后:2018-08-22 00:05:58
我尝试了一些毫无意义的问题。。。比如:
SELECT *
FROM history
WHERE (ABS(TIMESTAMPDIFF(DAY, date, "2018-08-19 00:08:34")) < 4) AND user_id = 1299
GROUP BY type
ORDER BY date
不管怎么说,仅使用MySQL查询就可以做到这一点吗?我需要使用一些应用程序逻辑来实现这一点吗?我如何质疑这一点
您可以在子查询上使用内部联接来实现最小差异
select
h.id, h.type
from
history
inner join
(select
type, min(TIMESTAMPDIFF(SECOND, date, '2012-06-06 15:20:18')) min_diff
from
history
where
(abs(TIMESTAMPDIFF(DAY, date, "2018-08-19 00:08:34")) < 4)
and user_id = 1299
group by
type) t on t.type = h.type
and TIMESTAMPDIFF(SECOND, h.date, '2012-06-06 15:20:18') = t.min_diff
这采用了一种标准的查询模式来查找每种类型都具有极值的行。在这种情况下,“极限”表示最接近目标值 首先,需要一个聚合子查询来获取极值:每种类型的行与目标日期之间的最小增量时间 然后将其连接到原始表,使用ON子句匹配类型和增量时间。把它们放在一起 编辑仅获取日期最近的未来或过去项目更容易。最近过去的子查询为
SELECT type, MAX(date) date
FROM history
WHERE date <= @target
GROUP BY date
然后整个查询是
SELECT history.*
FROM history
JOIN (
SELECT type, MAX(date) date
FROM history
WHERE date <= @target
GROUP BY date
) m ON history.type = m.type AND history.date = m.date
通过abstimestampdiffsecond,date,@dt:
这应该能奏效
create table test2 as
select c.id,c.type from(
select a.*,b.min
from test as a
left join
(select id, min(abs(date-"2018-09-01 00:00:00"dt)) as min from test
group by type ) as b
on a.id = b.id
) as c
where min = abs(date-"2018-09-01 00:00:00"dt)
order by id;
我们将id上的join返回到原始表min abs datediff,然后选择所需的记录。它没有给出正确的结果。它似乎只提供最新的数据。如果我需要添加额外的过滤。我可以这样做:选择历史。*从历史中加入选择类型,MINABSTIMESTAMPDIFFMINUTE,date,@target delta FROM history,其中user_id=1299按历史上的delta类型分组。type=delta.type和ABSTIMESTAMPDIFFMINUTE,date,@target=delta其中user_id=1299是否也可以只获取最近的未来或过去?不是两者都有。@HABO我没有用tsql标记它。
SELECT type, MAX(date) date
FROM history
WHERE date <= @target
GROUP BY date
SELECT history.*
FROM history
JOIN (
SELECT type, MAX(date) date
FROM history
WHERE date <= @target
GROUP BY date
) m ON history.type = m.type AND history.date = m.date
select @rn := 0, @dt := cast('2018-08-19 04:52:00' as datetime);
select id, type from (
select id,
type,
data,
date,
@rn := @rn + 1 rn
from history
order by abs(timestampdiff(second, date, @dt))
) a where rn = 1;
create table test2 as
select c.id,c.type from(
select a.*,b.min
from test as a
left join
(select id, min(abs(date-"2018-09-01 00:00:00"dt)) as min from test
group by type ) as b
on a.id = b.id
) as c
where min = abs(date-"2018-09-01 00:00:00"dt)
order by id;