如何将日期和时间从不同的MySQL列组合到一个完整的DateTime进行比较? [英] How to combine date and time from different MySQL columns to compare to a full DateTime?

查看:124
本文介绍了如何将日期和时间从不同的MySQL列组合到一个完整的DateTime进行比较?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

d 是DATE,列 t 是时间,列 v 是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')



<有什么更好的想法?也许在MySQL中有一个功能吗?没有什么像

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'

这篇关于如何将日期和时间从不同的MySQL列组合到一个完整的DateTime进行比较?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆