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 求TYPCE母转母转接头24PIN线路板图
  • ¥100 国外网络搭建,有偿交流
  • ¥15 高价求中通快递查询接口
  • ¥15 解决一个加好友限制问题 或者有好的方案
  • ¥15 急matlab编程仿真二阶震荡系统
  • ¥20 TEC-9的数据通路实验
  • ¥15 ue5 .3之前好好的现在只要是激活关卡就会崩溃
  • ¥50 MATLAB实现圆柱体容器内球形颗粒堆积
  • ¥15 python如何将动态的多个子列表,拼接后进行集合的交集
  • ¥20 vitis-ai量化基于pytorch框架下的yolov5模型