开发者

select questions which doesnot have answers

I have a simple query doubt.

Question Table
qid question
1   ques1
2   ques2
3   ques3
4   ques4
5   ques5
6   ques6
7   ques7

Answer Table
ansid qid answer
1     1   ans1
2     2   ans2
3     2   ans3
4     4   a开发者_如何学编程ns4
5     6   ans5

I have two tables. One for questions and one for answers. Question id(qid) is use as a foreign key in answer table. I want select questions which doesnot have an answer in the answer table. In the above example I need questions 3,5,7. My database is large it may contain more than 50,000 records.

Thanks ARun


select q.* from question as q
left join answer as a
on q.id = a.qid
where a.qid is null

edit. Moreover it would be better to add an index on answer table

alter table answer add index iq (qid);


select * from question where qid not in 
(select qid from answer)
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜