2013-08-21 84 views
5

我有2個表格:photolike。我試圖根據一天中喜歡的人數獲得流行的照片。所以基本上像'今天流行'基於不同時間級別的MySQL熱門排行

SELECT 
    p.id AS id, COUNT(li.id) AS total_likes \ 
FROM `photo` p \ 
    LEFT JOIN `like` li \ 
    ON p.id = li.photo_id \ 
WHERE 
    li.date > DATE_SUB(CURDATE(), INTERVAL 1 DAY) \ 
GROUP BY \ 
    p.id 

這個效果很好,當今天有足夠的喜歡數量。但如果過去一天沒有喜歡,它將不會返回記錄。

我也想改變這一點。是否有可能達到關卡?例如:基於多天
排名照片:

1. Get photos based on how many likes today 
2. Get photos based on how many likes for last week 
and so on... 

所以基本上這樣做是什麼,比方說,我們需要得到30個項目。首先它會嘗試根據今天有多少喜歡來獲取行。它可以是任何數字20,15等。然後它將得到剩餘的行數達到30,但現在將根據一週內有多少喜歡進行排序。

So something like: 
SELECT FROM photo SORT BY likes today, likes in a week ... 

感謝您的幫助!

+0

位置更改到AND – Strawberry

+0

DDL和/或sqlfiddle可能會幫助 – Strawberry

回答

3

您可以基於CASE計算不同的計數,例如,過去30天的最後一天計數。上週最近30天:

SELECT 
    p.id AS id 
    ,COUNT(CASE WHEN li.DATE > DATE_SUB(CURDATE(), INTERVAL 1 DAY) THEN li.id END) AS daily_likes 
    ,COUNT(CASE WHEN li.DATE > DATE_SUB(CURDATE(), INTERVAL 7 DAY) THEN li.id END) AS weekly_likes 
    ,COUNT(li.id) AS total_likes 
FROM `photo` p 
JOIN `LIKE` li 
    ON p.id = li.photo_id 
WHERE 
    li.DATE > DATE_SUB(CURDATE(), INTERVAL 30 DAY) 
GROUP BY 
    p.id 
ORDER BY daily_likes DESC, weekly_likes DESC, total_likes DESC 
LIMIT 30 

我不知道哪個清晰度的限制是基於,它可能是這樣的

SELECT * 
FROM 
(
    SELECT 
    p.id AS id 
    ,COUNT(CASE WHEN li.DATE > DATE_SUB(CURDATE(), INTERVAL 1 DAY) THEN li.id END) AS daily_likes 
    ,COUNT(CASE WHEN li.DATE > DATE_SUB(CURDATE(), INTERVAL 7 DAY) THEN li.id END) AS weekly_likes 
    ,COUNT(li.id) AS total_likes 
    FROM `photo` p 
    JOIN `LIKE` li 
     ON p.id = li.photo_id 
    WHERE 
    li.DATE > DATE_SUB(CURDATE(), INTERVAL 30 DAY) 
    GROUP BY 
    p.id 
) AS dt 
ORDER BY 
    case when daily_likes > 20 then daily_likes else 0 end desc, 
    case when weekly_likes > 100 then weekly_likes else 0 end desc, 
    total_likes DESC 
LIMIT 30 
+0

這是有道理的。所以你的第二個訂單的代碼設置最低喜歡的要求?或者我誤解了它? – pewpewlasers

+0

您訂購的第二個代碼對我來說不起作用,它給了我一個'Reference'daily_likes'不支持(參考組函數)'錯誤 – pewpewlasers

+1

是的,你是正確的,只有daily_likes> 20且weekly_likes> 100被使用。但MySQL似乎不支持ORDER BY中的別名:-(更改我的答案以使用派生表 – dnoeth

1

你試過聯盟嗎?

technet.microsoft

Techonthenet.com

Wikipedia

和NOT EXISTS

MySQL

藉助UNION你可以加入選擇幾個SELECT語句,只要列是相同的

從我的頭頂,像

(select p.id as id, 
     count(li.id) as total_likes 
    from photo p, 
     like li 
where p.id = li.photo_id 
    and li.date > date_sub(curdate(), interval 1 day) 
    and rownum <= 10 
order by total_likes 
group by p.id) 
union 
(select p.id as id, 
     count(li.id) as total_likes 
    from photo p, 
     like li 
where p.id = li.photo_id 
    and li.date > date_sub(curdate(), interval 7 day) 
    and rownum <= 10 
    and not exists (select p.id as id, 
        count(li.id) as total_likes 
        from photo p, 
          like li 
        where p.id = li.photo_id 
         and li.date > date_sub(curdate(), interval 1 day) 
         and rownum <= 10 
        order by total_likes 
        group by p.id) 
order by total_likes 
group by p.id) 

東西沒有測試過這一點,但想這樣的事情會做的伎倆

+1

聯合工作,但存在一個小問題:如果一張照片今天和一週前被喜歡,則結果的行將有兩次相同的照片 – pewpewlasers

+1

請找到另一個資源,而不是W3S,因爲它很糟糕,如果您覺得不好你促進它。 –

+2

http://www.w3fools.com/ – fancyPants

0

你可以計算今天之間經過的週數和'喜歡'約會和他們分組。

SELECT 
    p.id AS id, COUNT(li.id) AS total_likes, period \ 
FROM `photo` p \ 
    LEFT JOIN `like` li \ 
    ON p.id = li.photo_id \ 
WHERE 
    DATEDIFF(li.date, now())+7)%7 AS period \ 
GROUP BY \ 
    p.id, period 
ORDER by period, total_likes DESC 
LIMI 30