简体   繁体   English

mysql查询检查大于日期和时间

[英]mysql query to check greater than date and time

I have two date and time columns in my database. 我的数据库中有两个日期和时间列。 I want to get rows where date is greater than or equal to current date and current time 我想获取日期大于或等于当前日期和当前时间的行

I have used below query to get result 我用下面的查询来获取结果

SELECT DISTINCT     
A.appt_fromUser,A.date,A.time,A.appt_styleType,B.*,A.appt_shoutID,A.shout_status 
from appoint_db A 
INNER JOIN checkedin_db B ON A.appt_id=B.appid 
where A.appt_toUser='$userid' 
and A.date>='$date' 
and InChair='0' 
and A.time>='$time'

However this isn't working as I'd hoped. 但是,这不符合我的期望。

Eg, if I have two rows: 例如,如果我有两行:

2012/05/30 Time:6.00 2012/05/30时间:6.00
2012/05/30 Time:13.00 2012/05/30时间:13.00

And I query this with date of 2012/05/02 and Time:12:00, it returns only only the row where the time is greater than 12:00 我用日期2012/05/02和Time:12:00进行查询,它仅返回时间大于12:00的行

You should only check the time if the date is the same as the one you're checking against. 如果日期与要检查的日期相同,则只应检查时间。

SELECT DISTINCT     
A.appt_fromUser,A.date,A.time,A.appt_styleType,B.*,A.appt_shoutID,A.shout_status
from appoint_db A
INNER JOIN checkedin_db B ON A.appt_id=B.appid 
where A.appt_toUser='$userid'
and   InChair='0'
and (
       A.date>'$date'
       OR (
           A.date = '$date'
           AND
           A.time>='$time'
       )
    )

You could also concatenate the date and time before selecting. 您还可以在选择之前连接日期和时间。 I wouldn't though (might not work with some locales). 我不会(可能无法在某些语言环境下工作)。

SELECT DISTINCT
A.appt_fromUser,A.date,A.time,A.appt_styleType,B.*,A.appt_shoutID,A.shout_status
from appoint_db A
INNER JOIN checkedin_db B ON A.appt_id=B.appid
where A.appt_toUser=1
and InChair='0'
AND CONCAT(A.date, ' ', A.time) >= '$date $time'

The now() function in mysql returs current timestamp . mysql中的now()函数返回当前时间戳。 you can use that 你可以用那个

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM