使用列中不同值的数量作为选择条件

时间:2015-01-07 15:30:02

标签: mysql sql select relational-database

我在mysql数据库中有以下架构

Field       |  Type   |    Null  |  Key  |  Default  |  Extra
------------+---------+----------+-------+-----------+---------
answerId     int(11)      NO       PRI     NULL
answerDate   datetime     YES              NULL
creationDate datetime     YES              NULL
questionId   int(11)      NO       PRI     0
url          text         YES              NULL
description  text         YES              NULL

该表存储问题和答案的关系,其中一个问题与一个或多个答案有关。我想做的是选择超过5个答案的问题。是否有可能检索到它?

3 个答案:

答案 0 :(得分:2)

它会对你有用吗?

SELECT questionId
FROM question_answer
GROUP BY questionId
HAVING COUNT(*) >5 

旁注。其他一些属性urlcreationDatedescription似乎并没有描述问答之间的关系......

答案 1 :(得分:1)

Select questionID, count(*) as totalAnswers from qanda
group by questionID
having totalAnswers > 5

Example SQL Fiddle

答案 2 :(得分:0)

 SELECT *
 FROM table 
 GROUP BY questionId   
 HAVING count(questionId)> 5

sql fiddle