在两个表

时间:2016-04-02 15:40:46

标签: mysql

我有两张这样的表:

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

1 个答案:

答案 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