2011-11-30 57 views
0

我目前的投票值與「投票」表免受「故事」表文章總結獲得「最精彩的文章。」如何從數據庫中選擇日,周,月,年的信息?

當前PHP查詢:

$sql = mysql_query("SELECT stories.*, SUM(votes.vote_value) as 'total_votes' FROM stories JOIN votes ON stories.id = votes.item_name GROUP BY stories.id ORDER BY total_votes DESC LIMIT 10") or die (mysql_error("There was an error in connection")); 

數據庫結構:

故事表:

img1

票表:

img2


但我想知道如何來顯示不同的時間段內的信息。說顯示最高評分文章「今天」「上週」「上個月」「去年」。從「故事」表基礎上的文章創建日期st_date。我只需要那些負責在不同時間段內獲取數據的4個(今天,上週,每月,每年)不同的查詢或部分。

回答

1
$date1 = "TO_DAYS(NOW()) - TO_DAYS(stories.st_date) = 0"; 
$date2 = "TO_DAYS(NOW()) - TO_DAYS(stories.st_date) <= 7"; 
$date3 = "TO_DAYS(NOW()) - TO_DAYS(stories.st_date) <= 31"; 
$date4 = "TO_DAYS(NOW()) - TO_DAYS(stories.st_date) <= 365"; 

$query = "SELECT stories.*, SUM(votes.vote_value) as 'total_votes' FROM stories JOIN votes ON stories.id = votes.item_name WHERE ".$date1." GROUP BY stories.id ORDER BY total_votes DESC LIMIT 10" 
$query = "SELECT stories.*, SUM(votes.vote_value) as 'total_votes' FROM stories JOIN votes ON stories.id = votes.item_name WHERE ".$date2." GROUP BY stories.id ORDER BY total_votes DESC LIMIT 10" 
$query = "SELECT stories.*, SUM(votes.vote_value) as 'total_votes' FROM stories JOIN votes ON stories.id = votes.item_name WHERE ".$date3." GROUP BY stories.id ORDER BY total_votes DESC LIMIT 10" 
$query = "SELECT stories.*, SUM(votes.vote_value) as 'total_votes' FROM stories JOIN votes ON stories.id = votes.item_name WHERE ".$date4." GROUP BY stories.id ORDER BY total_votes DESC LIMIT 10" 
+0

@Ilya Knaup一個好主意:它的工作原理? –

+0

嗨$ date4有365天,如果有366天,這有什麼關係嗎?我的意思是它會搞砸了還是很好?日Thnx爲真棒ANS)) – Ilja

+0

根據http://www.compshack.com/sql-plsql/find-number-days-year:'ADD_MONTHS(TRUNC(SYSDATE, 'Y'),12)-TRUNC(SYSDATE, 'Y')'。我不試試它是否有效。也許在PHP中更容易。 –

1
SELECT stories.*, SUM(votes.vote_value) as 'total_votes' 
FROM stories 
JOIN votes 
    ON stories.id = votes.item_name 
WHERE (
     (st_date BETWEEN (NOW(), DATE_SUB(NOW(), INTERVAL 7 DAY)) 
     OR 
     (st_date BETWEEN (DATE_SUB(NOW(), INTERVAL 14 DAY, DATE_SUB(NOW(), INTERVAL 7 DAY)) 
    ) 
GROUP BY stories.id 
ORDER BY total_votes DESC LIMIT 10") 
1

快速修正一些上述問題的答案。我相信OP正在尋找AVG()而不是SUM(),因爲可能有1000票與1值和100與5,所以要獲得最高評價,你需要AVG。要獲得「最常見的評分」,你可以做COUNT()。 SUM()本身實際上並沒有告訴你很多。 200x5 = 1000x1 = 300x3 + 100X1

如果更改st_date到時間戳,你也可以在除了其他的解決方案中提到的其他日期函數使用TIMESTAMPDIFF。改變之日起,以月或年和 1月/年或剛剛離開的日子,做365分之30

$query = "SELECT stories.*, AVG(votes.vote_value) as 'total_votes' 
FROM stories JOIN votes ON stories.id = votes.item_name 
WHERE abs(timestampdiff(DAY, stories.st_date, NOW())) < 7 
GROUP BY stories.id ORDER BY total_votes DESC LIMIT 10" 

而且,上面的查詢讓你內創建的收視率最高的文章期望的時期。如果您決定看的時間內收視率最高的文章,當它們被創造NO MATTER,改變你的where子句

$query = "SELECT stories.*, AVG(votes.vote_value) as 'total_votes' 
FROM stories JOIN votes ON stories.id = votes.item_name 
WHERE abs(timestampdiff(DAY, votes.date, NOW())) < 7 
GROUP BY stories.id ORDER BY total_votes DESC LIMIT 10" 

匹配投票日期您可以結合這兩種物品的創建和一個查詢中也投票期。

最後但並非最不重要的一點,您應該將'date'列的名稱更改爲vote_date之類的其他名稱。日期是一個保​​留字,它不使用,對列名

相關問題