筛选记录与当前日期

问题描述 投票:0回答:3

我被陷在数据库查询。我有两个列一个日期和其他时间的表。

我想筛选记录与当前日期时间意味着如果只有这些记录会显示那些包含日期和时间值大于或等于当前日期时间。能否请你在写这篇SQL查询引导?谢谢

mysql sql
3个回答
3
投票

您可以使用:

where date > curdate() or
      (date = curdate() and time > curtime())

0
投票

另一种选择是简单地串联的日期和时间。在MySQL中,这是生成日期的简单明了的方式:

where CONCAT(date, ' ', time) > NOW()

注:如由胡安·卡洛斯·Oropeza评论,这表达不允许你使用的datetime(如果有的话)列现有的索引。如果你正在处理一个非常大的数据量和/或性能关键的问题给你的,这种解决方案可能不会扩大由戈登·利诺夫解决好。


0
投票
create table Test(id integer, title varchar(100), date DATE , time TIME);

insert into Test(id, title, date, time) values(1, "Date one", '2019-01-01', '01:20:00');
insert into Test(id, title, date, time) values(2, "Date two", '2019-01-02', '11:40:00');
insert into Test(id, title, date, time) values(3, "Date three", '2019-01-03', '23:56:00');
insert into Test(id, title, date, time) values(4, "Date foure", '2019-01-04', '12:20:00');
insert into Test(id, title, date, time) values(5, "Date five", '2019-01-05', '01:20:00');
insert into Test(id, title, date, time) values(6, "Date six", '2019-01-12', '15:47:00');
insert into Test(id, title, date, time) values(7, "Date seven", '2019-01-21', '10:20:00');
insert into Test(id, title, date, time) values(8, "Date eight", '2019-02-01', '01:20:00');


select * from Test WHERE date > '2019-01-04' OR (date = '2019-01-04' AND time >= '10:00:00');
© www.soinside.com 2019 - 2024. All rights reserved.