2015-10-13 57 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值少。 –