如何将日期和时间从不同的MySQL列组合到一个完整的DateTime进行比较?

问题描述:

d 是DATE,列 t 是时间,列 v $例如,c $ c>是INT。假设我需要在2012年2月1日15:00之后记录的所有值。如果我写了

Column d is DATE, column t is time, column v is, for example, INT. Let's say I need all the values recorded after 15:00 of 01 Feb 2012 and on. If I write

SELECT * FROM `mytable` WHERE `d` > '2012-02-01' AND `t` > '15:00'

在任何日期15:00之前所做的所有记录都将被排除从结果集(以及所有在2012-02-01),而我想看到他们。如果有一个DATETIME列,似乎很简单,但是对于日期和时间而言,这个列是单独的列,而在我的情况下。

all the records made before 15:00 at any date are going to be excluded from the result set (as well as all made at 2012-02-01) while I want to see them. It seems it would be easy if there were a single DATETIME column, but there are separate columns for date and time instead in the case of mine.

最好的我可以看到现在是像

The best I can see now is something like

SELECT * FROM `mytable` WHERE `d` >= '2012-02-02' OR (`d` = '2012-02-01' AND `t` > '15:00')



Any better ideas? Maybe there is a function for this in MySQL? Isn't there something like

SELECT * FROM `mytable` WHERE DateTime(`d`, `t`) > '2012-02-01 15:00'

可能?

可以使用mysql CONCAT()函数将两列添加到一起,然后比较他们喜欢这样:

You can use the mysql CONCAT() function to add the two columns together into one, and then compare them like this:

SELECT * FROM `mytable` WHERE CONCAT(`d`,' ',`t`) > '2012-02-01 15:00'