我有两张桌子。一部附影片(lol_videos):

ID,标题,网址...

和命中表(lol_hits):

ID,视频ID,时间戳

我想根据视频表中的点击次数(存储在lol_hits中)和在特定时间范围内对视频进行排序。

我已经尝试了一些不同的东西,但我真的不能对..到目前为止,我只是遇到了一些无用的mysql错误:(

$query = "SELECT `lol_videos`.*, COUNT(lol_hits.videoID) as hit_count".
" FROM `lol_videos`".
" WHERE published > $now AND pushed > 0".
" LEFT JOIN `lol_hits`".
" ON (`lol_hits`.videoID = `lol_videos`.ID)".
" ORDER BY hit_count DESC";
$result = mysql_query($query) or die(mysql_error());


有人建议如何解决这个问题?

最佳答案

这应该工作:

SELECT v.*, v2.numHits FROM (
    SELECT videoID, COUNT(ID) 'numHits' FROM lol_hits GROUP BY videoID
) AS v2
JOIN lol_videos v ON (v.ID = v2.videoID)
ORDER BY v2.numHits DESC;

关于mysql - 表中的行顺序取决于相对表,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/9004228/

10-11 20:52