我的数据库中有2个表,第一个表称为“问题”,表示持有id的问题,第二个表称为“答案”并保留问题的答案(作为多个选项)。
如何选择少于4个答案的问题?
问题表:
id question
1 what is ...?
2 how many ...?
3 Is ....?
答案表
id question_id answer
1 1 54
2 1 11
3 1 22
4 2 England
5 1 5
6 2 Turkey
如何选择答案少于4的问题?
感谢,
答案 0 :(得分:3)
select questions.id, questions.question from questions
inner join answers on questions.id = answers.question_id
group by questions.id, questions.question having count(questions.id) <4
你走了。
答案 1 :(得分:0)
使用它:
SELECT *
FROM questions
WHERE id in
(
SELECT question_id
from answers
group by question_id
having count(question_id) <4
)