我有两张这样的表:
ideas
------------
id
title
description
和
votes
------------
id
ideas_id
我想做一个选择以获得投票的最终结果,排序。每次投票都代表一个观点。
我试过了:
SELECT count(distinct votes.ideas_id),ideas.title, ideas.description从投票INNER JOIN想法ON votes.ideas_id = ideas.id
答案 0 :(得分:1)
试试这个:
select ideas_id,vote_count,title,description
from ideas inner join
(select count(ideas_id) as vote_count,ideas_id from votes group by ideas_id) as T
ON T.ideas_id=ideas.id
ORDER BY vote_count desc