PHP评分最高的故事

时间:2010-12-21 17:52:25

标签: php vote-up-buttons

我有两张桌子:

故事 ID(int),CONTENT(文本)

ID(int),TYPE(int,1或0),ID_STORY(int)

如何让查询返回按投票(= 1)desc排序的前10个故事?我希望能够打印十大故事内容。

我已尝试过很多类似问题的解决方案,但我无法做到这一点......

2 个答案:

答案 0 :(得分:2)

SELECT *, count(votes) AS vcount
  FROM stories s, votes v
 WHERE s.id=v.id_story
   AND v.type=1
 GROUP BY v.id_story
 ORDER BY vcount DESC

答案 1 :(得分:0)

SELECT 
    storyid,content 
FROM 
    stories 
WHERE 
    storyid IN (
        SELECT 
            storyid,count(votes) AS count 
        FROM 
            stories LEFT JOIN votes ON stories.storyid=votes.storyid 
        WHERE 
            type=1 
            GROUP BY votes.storyid 
            ORDER BY count DESC
)