简体   繁体   English

如何仅选择大于当前日期和时间的行?

[英]How do I SELECT only rows that are greater than today's date and time?

Forgive me if this is a simple question, but I am trying to echo out rows that are only in the future, ie: greater than today's date and time. 如果这是一个简单的问题,请原谅我,但我试图回应仅在未来的行,即:大于今天的日期和时间。 This is my code so far: 到目前为止这是我的代码:

$date = date('Y-m-d');
$time = date("G:i:s", time());

$eventsquery = "SELECT * FROM events as A WHERE tech='$userecho' AND date >= '$date' ORDER BY date ASC, time ASC";
$eventsresult = mysql_query($eventsquery, $eventsdbhandle);

This works great for giving me rows that are either for today or in the future, This problem is, this will give me entries that happened before the current time on the current day. 这非常适合为我提供今天或将来的行。这个问题是,这将为我提供当前时间之前发生的条目。 So I tried adding this: 所以我尝试添加这个:

$eventsquery = "SELECT * FROM events as A WHERE tech='$userecho' AND date >= '$date' AND time >= '$time' ORDER BY date ASC, time ASC";

But I get no rows at all. 但我根本没有任何行。 I know this is likely an easy fix, but I have found nothing on this site that fixes it. 我知道这可能很容易解决,但我在这个网站上找不到修复它的东西。 Any help would be great. 任何帮助都会很棒。

Time in the table is stored in this format: hh:mm:ss ie: 14:23:12 表格中的时间以这种格式存储:hh:mm:ss ie:14:23:12

One way to do it 一种方法

SELECT * 
  FROM events
 WHERE tech = ?
   AND ADDTIME(date, time) > NOW()
 ORDER BY ADDTIME(date, time)

Here is SQLFiddle demo 这是SQLFiddle演示

If you have 1 column of datetime value you can say 如果您有1列日期时间值,您可以说

"SELECT * FROM table_name WHERE entry_time > NOW()"

However, it gets complicated if you have two different columns for date and time each You will have to use 但是,如果您有两个不同的日期和时间列,则必须使用它们

"SELECT * FROM table_name WHERE entry_date > CURDATE() OR (entry_date = CURDATE() AND entry_time > CURTIME()"

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

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