dongsanhu4784 2013-10-05 21:39
浏览 203
已采纳

Mysql查询结果从当前日期起不超过一个月

Hello I have a database which has a field date inside it which has the following format: 2013-09-03 for example. I am making a mysql query which is displaying results from this database but I would like to modify it this way so it will display only the results which data is not more than 31 days from today or one month , where today must me the date of today get from the server, here is the query so far: if

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

for example if today is first of january the results which the query should show are which have date no bigger than 1 oF february.

any help will be appreciated. Thanks

  • 写回答

2条回答 默认 最新

  • dozxos6346 2013-10-05 21:44
    关注

    Try

    (!$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"));

    However, it would be better to pre-calculate the cutoff date to avoid MYSQL having to do the DATEDIFF for all rows, so something like:

    $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'"));

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥30 win from 窗口最大最小化,控件放大缩小,闪烁问题
  • ¥20 易康econgnition精度验证
  • ¥15 msix packaging tool打包问题
  • ¥28 微信小程序开发页面布局没问题,真机调试的时候页面布局就乱了
  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致