dongmei9203 2011-11-20 14:08
浏览 14
已采纳

SQL SELECT日期从10天前失败

I'm havig some trouble with SQL:

First, I insert the current date, using PHP-time():

mysql_query("
   INSERT INTO applicants (
      date_last_applied
   )
   VALUES (
      " . time() . "
   )
");

This works just fine. However, I only want to execute an action when that ^ was inserted 10 days ago:

$result = mysql_query("
   SELECT * FROM applicants WHERE date_last_applied < DATE_SUB(NOW(), INTERVAL 10 DAY)
");
while ($row = mysql_fetch_array($result)) 
    echo $row['id_member'];

Although it should work (IMO), it doesn't. D:

For possible solutions: it's not an option to use an other timeformat, because I'm using this format on my entire site.

  • 写回答

4条回答 默认 最新

  • doushuo1080 2011-11-21 08:26
    关注

    Ok, thanks for your help, y'all. This code works flawlessly:

     //get member's info
    $result = mysql_query("
       SELECT * FROM DDFS_applicants 
       WHERE FROM_UNIXTIME(date_last_applied, '%Y %D %M') = FROM_UNIXTIME(UNIX_TIMESTAMP(DATE_SUB(NOW(), INTERVAL 1 DAY)), '%Y %D %M')
    ");
    while ($row = mysql_fetch_array($result)) {
        echo $row['id_member'];
    

    (So it works even to check if the date is the exact same date. If you want, you can even add hours to it, but I chose not to.)

    Source: http://dev.mysql.com/doc/refman/5.1/en/date-and-time-functions.html#function_date-sub

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

报告相同问题?

悬赏问题

  • ¥15 各位请问平行检验趋势图这样要怎么调整?说标准差差异太大了
  • ¥15 delphi webbrowser组件网页下拉菜单自动选择问题
  • ¥15 wpf界面一直接收PLC给过来的信号,导致UI界面操作起来会卡顿
  • ¥15 init i2c:2 freq:100000[MAIXPY]: find ov2640[MAIXPY]: find ov sensor是main文件哪里有问题吗
  • ¥15 运动想象脑电信号数据集.vhdr
  • ¥15 三因素重复测量数据R语句编写,不存在交互作用
  • ¥15 微信会员卡等级和折扣规则
  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab