doo58088 2017-11-29 01:01
浏览 210
已采纳

具有动态WHERE时间戳的SQL查询[重复]

This question already has an answer here:

How can i get this part of a query to fetch the values by timerange?

Example of a table:

  select * from tablename where timeregister between '2017-01-11' And '2017-01-30';

How could i get the timeregister to be last 7 days? I was trying this:

select * from tablename where timeregister(NOW(), timeregister) <= 7;
</div>
  • 写回答

3条回答 默认 最新

  • douyao4632 2017-11-29 01:07
    关注

    Try this....

    <?php 
        $s_date = date('Y-m-d');
        $e_date = date('Y-m-d',strtotime('+7 days'));
        $sql = 'SELECT * FROM tablename WHERE DATE_FORMAT(timeregister, "%Y-%m-%d") BETWEEN "'.$s_date.'" AND "'.$e_date.'"';
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)
编辑
预览

报告相同问题?

悬赏问题

  • ¥15 DevEco studio开发工具 真机联调找不到手机设备
  • ¥15 请教前后端分离的问题
  • ¥100 冷钱包突然失效,急寻解决方案
  • ¥15 下载honeyd时报错 configure: error: you need to instal a more recent version of libdnet
  • ¥15 距离软磁铁一定距离的磁感应强度大小怎么求
  • ¥15 霍尔传感器hmc5883l的xyz轴输出和该点的磁感应强度大小的关系是什么
  • ¥15 vscode开发micropython,import模块出现异常
  • ¥20 Excel数据自动录入表单并提交
  • ¥30 silcavo仿真,30分钟,只需要代码
  • ¥15 FastReport 怎么实现打印后马上关闭打印预览窗口