您可以结合使用来自不同MySQL列的日期和时间,以借助CONCAT()
方法与整个日期时间进行比较。语法如下-
SELECT *FROM yourTableName WHERE CONCAT(yourDateColumnName,'',yourTimeColumnName) > 'yourDateTimeValue';
为了理解上述语法,让我们创建一个表。创建表的查询如下-
mysql> create table DifferentDateTime -> ( -> Id int NOT NULL AUTO_INCREMENT PRIMARY KEY, -> ArrivalDate date, -> ArrivalTime time -> );
使用insert命令在表中插入一些记录。查询如下-
mysql> insert into DifferentDateTime(ArrivalDate,ArrivalTime) values(curdate(),'10:27:30'); mysql> insert into DifferentDateTime(ArrivalDate,ArrivalTime) values('2016-04-24','15:00:00'); mysql> insert into DifferentDateTime(ArrivalDate,ArrivalTime) values('2015-12-25','12:30:30'); mysql> insert into DifferentDateTime(ArrivalDate,ArrivalTime) values('2012-01-31','16:00:30'); mysql> insert into DifferentDateTime(ArrivalDate,ArrivalTime) values('2000-07-12','09:45:31');
使用select语句显示表中的所有记录。查询如下-
mysql> select *from DifferentDateTime;
输出结果
+----+-------------+-------------+ | Id | ArrivalDate | ArrivalTime | +----+-------------+-------------+ | 1 | 2019-02-14 | 10:27:30 | | 2 | 2016-04-24 | 15:00:00 | | 3 | 2015-12-25 | 12:30:30 | | 4 | 2012-01-31 | 16:00:30 | | 5 | 2000-07-12 | 09:45:31 | +----+-------------+-------------+ 5 rows in set (0.00 sec)
这是查询以合并来自不同列的日期和时间并与日期时间值进行比较的查询-
mysql> select *from DifferentDateTime where concat(ArrivalDate,'',ArrivalTime) > '2007-01-31 12:30:30';
输出结果
+----+-------------+-------------+ | Id | ArrivalDate | ArrivalTime | +----+-------------+-------------+ | 1 | 2019-02-14 | 10:27:30 | | 2 | 2016-04-24 | 15:00:00 | | 3 | 2015-12-25 | 12:30:30 | | 4 | 2012-01-31 | 16:00:30 | +----+-------------+-------------+ 4 rows in set (0.00 sec)