dops57958 2015-11-07 17:17 采纳率: 0%
浏览 38
已采纳

MySQL在给定间隔之间选择行

table videos

id | title  | time
------------------------
1  | o'najr | 1406332800

I want to select rows which are between the interval of 6 months from a given time. Both newer or older

So if the query time is for ex. 1446076800 all the rows which 6 months older or 6 months newer than this will be selected.

time is in the UNIX TIMESTAMP format.

So if a video is uploaded on October 2015 all the videos which are uploaded on April 2015 and newer will be selected. Also all the videos from October 2015 up to April 2016 will be selected.

6 months <---- || query time || ----> 6 months

This is my query but it doesn't work at all. It just as a structure not a working query.

:time is the variable which will be used for the query.

select `title` 
from `videos` 
where DATE_FORMAT(FROM_UNIXTIME(time - :time)) > INTERVAL 6 MONTH 
      or 
      DATE_FORMAT(FROM_UNIXTIME(:time - time)) < INTERVAL 6 MONTH
  • 写回答

1条回答 默认 最新

  • duankousong9637 2015-11-07 17:27
    关注

    Your query doesn't quite make sense. You're trying to compare a specific date to a time interval.

    Try this:

    SELECT title FROM videos 
    WHERE FROM_UNIXTIME(time) BETWEEN
        DATE_SUB(FROM_UNIXTIME(:time), INTERVAL 6 MONTH) 
        AND DATE_ADD(FROM_UNIXTIME(:time), INTERVAL 6 MONTH)
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料
  • ¥15 使用R语言marginaleffects包进行边际效应图绘制
  • ¥20 usb设备兼容性问题
  • ¥15 错误(10048): “调用exui内部功能”库命令的参数“参数4”不能接受空数据。怎么解决啊
  • ¥15 安装svn网络有问题怎么办