2015-10-13 56 views
0

我有以下查询,我要选择所有从表,不超过6个月比选择视频的当前时间早的视频:time选择行不超过6个月的使用MySQL

我得到这个值15638400由此:echo strtotime('+6 month', 0);

表中的所有时间都是UNIX timestamp格式。

当我运行这个查询时,所有的视频都被选中,因为这行不在那里。所以,我的意思是结果呢取决于having time - :time < 15638400。如果我删除这个结果是相同的。

一个视频将被选中,它将有一个时间戳X为前。比这个查询将返回所有在与第一视频上传比较飞蛾6或更早的视频。因此,6个月或小于设定值X.

$query = "select `video_name`, `title`, `yt_id` from `videos` 
    where match(`genre`) against (+:genre IN BOOLEAN MODE) 
    and match(`country`) against (+:country IN BOOLEAN MODE) 
    having time - :time < 15638400 
    and `video_name` != :video_name ORDER BY RAND() limit :limit"; 

try { 
    $run_query = $db->prepare($query); 

    $run_query->bindValue(':country', $this->country); 
    $run_query->bindValue(':genre', $this->genre); 
    $run_query->bindValue(':time', $this->time); 
    $run_query->bindValue(':video_name', $this->video_name); 
    $run_query->bindValue(':limit', $limit, PDO::PARAM_INT); 

    $run_query->execute(); 

    return $run_query->fetchAll(PDO::FETCH_OBJ); 

} catch(PDOException $e) { 
    echo $e->getMessage(); 
} 

回答

1

您可以尝试

select id 
from your_table 
where your_timestamp_column <= (now() - interval 6 month); 
+0

我想将其与特定的视频,'的时间比较:不能以'现在time'变量() ' –

+0

现在()用于当前时间戳,您需要将your_timestamp_column替换为:time变量(列) – Disk01

+0

我认为您没有理解我的权利。一个视频将被选中,它将有一个时间戳X为前。比起第一个视频,这个查询将返回所有上传6个或更早的视频。因此,6个月或比X值少。 –