您好我有一个数据库,其中包含一个字段date
,其格式如下:2013-09-03
。我正在制作一个mysql查询,显示来自这个数据库的结果但是我想这样修改它所以它只显示数据从今天或一个月不超过31天的结果,今天必须是我的日期今天从服务器获取,这是目前为止的查询:if
(!$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日。
任何帮助将不胜感激。感谢
答案 0 :(得分:1)
尝试使用日期算术
WHERE ...
AND date <= TODAY() + INTERVAL 1 MONTH
答案 1 :(得分: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'"));