输出超过24小时未输入数据库的mysql值。
我试图加入辅助表并比较日期大于1天的日期输入,但是当我运行查询时,它似乎并未过滤掉过去24小时内输入的数据。 / p>
SELECT t1.value,t1.date,t1.time
FROM filter t1 LEFT JOIN filter t2
ON (t1.value= t2.value AND concat(t1.date,' ',t1.time) <
concat(t2.date,t2.time)) WHERE t2.date > curdate() - interval 1 day;
http://www.sqlfiddle.com/#!9/9d41ea/2
我的理想结果是使用下面的示例,其中日期为06/01/19
value date
1 01/01/19
1 05/01/19
2 05/01/19
2 06/01/19
3 03/01/19
3 04/01/19
4 05/01/19
4 06/01/19
过去一天(05/01/19和06/01/19)显示结果中数据库中不存在的选择值(数字)
过去一天1.2.4中显示的数字未显示在结果中
过去3天没有显示的数字显示在结果中
查询,例如
select value,date from filter where date < curdate() - interval 1 day but not > curdate() - interval 1 day
答案 0 :(得分:0)
在此示例中,我得到了在过去24小时内预期已进行交易的所有值,然后将其连接起来以查看是否具有这些值,
drop table if exists t;
CREATE TABLE t (
value VARCHAR(45),
date DATE,
time TIME
);
INSERT INTO t (value,date,time)
VALUES ('1','2019-01-10','14:30:00'),
('1','2019-01-10','14:30:00'),
('1','2019-01-13','14:31:00'),
('1','2019-01-14','14:32:00'),
('2','2019-01-10','14:30:00'),
('2','2019-01-13','14:31:00'),
('2','2019-01-14','14:32:00'),
('3','2019-01-10','14:30:00'),
('3','2019-01-13','14:31:00'),
('3','2019-01-14','14:32:00'),
('4','2019-01-10','14:30:00'),
('4','2019-01-13','14:31:00'),
('4','2019-01-14','14:32:00'),
('5','2019-01-01','14:00:00');
select s.value,s.dt,t.dt
from
(
select distinct value,last24hr.dt
from t
cross join (select date_sub(now(), interval 1 day) dt) last24hr
) s
left join
(select value,cast(concat(date,' ',time) as datetime) dt from t) t
on s.value = t.value and s.dt <= t.dt
where t.dt is null;
+-------+---------------------+------+
| value | dt | dt |
+-------+---------------------+------+
| 5 | 2019-01-13 12:08:23 | NULL |
+-------+---------------------+------+
1 row in set (0.00 sec)
您的示例当然都不会失败,因此我添加了5个,当您对此进行测试(如果您这样做)时可能找不到它