嘿,我已經在php和mysql中創建了評分系統,現在我想選擇前5名,我做了一些事情,但是如果用戶添加了評分(最大)它是nr 1排名。在php和mysql中計算前5名
你們是如何做到這一點在PHP和MySQL?
表看起來是這樣的:
- ID
- 中期
- UID
- 遊客
該評級是從1到數5
在此先感謝!
嘿,我已經在php和mysql中創建了評分系統,現在我想選擇前5名,我做了一些事情,但是如果用戶添加了評分(最大)它是nr 1排名。在php和mysql中計算前5名
你們是如何做到這一點在PHP和MySQL?
表看起來是這樣的:
- ID
- 中期
- UID
- 遊客
該評級是從1到數5
在此先感謝!
由於@chaos指出,這是想法:
SELECT `mid`, SUM(`rating`) AS `total`
FROM `rating`
GROUP BY `mid`
ORDER BY `total` DESC
LIMIT 5
然而,以確保不會與極少數評級物品進入前5,你可以添加一個門檻,只允許文章擁有超過X收視率將在結果顯示,也許能給前5名的更準確的圖片:
SELECT `mid`, SUM(`rating`) AS `total`, COUNT(1) AS `nrRatings`
FROM `rating`
GROUP BY `mid`
HAVING nrRatings > 5 // This is the threshold. Only articles with more than
// 5 ratings will be considered
ORDER BY `total` DESC
LIMIT 5
如果計分爲空,則COUNT(*)或COUNT(計分)即可,比「SUM(1)」更具慣用性。 – chaos 2009-07-20 20:27:52
SELECT `mid`, SUM(`rating`) AS `total`
FROM `rating`
GROUP BY `mid`
ORDER BY `total` DESC
LIMIT 5
如果我理解你的寫法,看起來像一個等級1的解釋第1條ST? ORDER BY應該是ASC。我認爲AVG()比SUM()更合適。與閾值的想法完全一致,所以這樣的事情(無恥的C/P重寫; THX @Machine:P):
SELECT `mid`, AVG(`rating`) AS `avg_rating`, SUM(1) AS `nrRatings` FROM `rating` GROUP BY `mid` HAVING nrRatings > 5 ORDER BY `avg_rating` ASC LIMIT 5
什麼是中期和uid?你跟蹤的票數?只需要最低數量的票數即可計入前5名。 – jimyi 2009-07-20 20:08:27