2014-02-28 338 views
-1

我有這樣的查詢索引我的圖片,並通過普及命令他們,但我不能讓用戶選擇的時間間隔的原因有什麼毛病查詢:MySQL查詢日期錯誤

switch($Data['data']){ 
    case 'daily':$QueryDate='=CURDATE()';break; 
    case 'weekly':$QueryDate=' BETWEEN SUBDATE(CURDATE(), INTERVAL 7 DAYS) AND NOW()';break; 
    case 'monthly':$QueryDate='>CURDATE() - INTERVAL 31 DAYS';break; 
    default: Core::redirect('image/browse/daily/1');break; 
} 
$IMGDB = new Database('images'); 
$query = "SELECT *, (derived.`likes` * 2 + derived.`views`) as `popularity` from 
(SELECT *, 
    (SELECT COUNT(*) FROM `likes` WHERE `like`=I.id AND `date`".$QueryDate.") AS `likes`, 
    (SELECT SUM(`views`) FROM `views` WHERE `id`=I.id AND `date`".$QueryDate.") AS `views` 
    FROM images AS I 
) AS derived 
where 1 ORDER BY `popularity` DESC "; 

只有日常情況下工作。

以下是錯誤:

SQL Error (1064): You have an error in your SQL syntax;..... to use near 'DAYS) AND NOW()) AS likes, (SELECT SUM(views) FROM views WHERE id= I.id A

+2

什麼是你的錯誤? –

+0

@TarynEast SQL錯誤(1064):您的SQL語法有錯誤; .....在'DAYS'和NOW())之間使用AS'likes',(SELECT SUM('views')FROM'views 'WHERE'id' = I.id A' –

+3

'INTERVAL 7 DAY',not'DAYS'。 – Sammitch

回答

1

用於指定天的間隔正確的語法使用DAY關鍵字。你已經使用了DAYS在:

BETWEEN SUBDATE(CURDATE(), INTERVAL 7 DAYS) AND NOW() 

和:

> CURDATE() - INTERVAL 31 DAYS