【发布时间】:2010-12-21 17:52:25
【问题描述】:
我有 2 张桌子:
故事 ID (int), CONTENT (文本)
投票 ID (int), TYPE (int, 1 or 0), ID_STORY (int)
如何让查询返回按投票 (=1) desc 排序的前 10 个故事?我希望能够打印前 10 个故事内容。
我已经尝试了很多这里提供的类似问题的解决方案,但我无法做到正确......
【问题讨论】:
标签: php vote-up-buttons
我有 2 张桌子:
故事 ID (int), CONTENT (文本)
投票 ID (int), TYPE (int, 1 or 0), ID_STORY (int)
如何让查询返回按投票 (=1) desc 排序的前 10 个故事?我希望能够打印前 10 个故事内容。
我已经尝试了很多这里提供的类似问题的解决方案,但我无法做到正确......
【问题讨论】:
标签: php vote-up-buttons
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
【讨论】:
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
)
【讨论】: