0

您好,我有一个数据库,其中有一个字段date,格式如下:2013-09-03例如。我正在做一个 mysql 查询,它显示来自这个数据库的结果,但我想以这种方式修改它,以便它只显示数据不超过今天或一个月后 31 天的结果,今天必须是我的日期今天从服务器获取,这是目前为止的查询:如果

(!$result = mysqli_query($con,
 "SELECT id, title, date, date_until, content FROM dashboard WHERE city = 'VC' AND type = 'class' AND  status = '1'"))

例如,如果今天是 1 月 1 日,则查询应显示的结果是日期不大于 2 月 1 日的结果。

任何帮助将不胜感激。谢谢

4

2 回答 2

1

尝试使用日期算术

 WHERE ...
   AND date <= TODAY() + INTERVAL 1 MONTH
于 2013-10-05T21:43:17.977 回答
0

尝试

(!$result = mysqli_query($con,"SELECT id, title, date, date_until, content FROM dashboard WHERE city = 'VC' AND type = 'class' AND status = '1' AND DATEDIFF(date, NOW()) <= 31"));

但是,最好预先计算截止日期以避免 MYSQL 必须对所有行执行 DATEDIFF,例如:

$cutoff = date("Y-m-d", strtotime("+31 day"); (!$result = mysqli_query($con,"SELECT id, title, date, date_until, content FROM dashboard WHERE city = 'VC' AND type = 'class' AND status = '1' AND date <= '$cutoff'"));

于 2013-10-05T21:44:15.917 回答