开发者

select questions which doesnot have answers

开发者 https://www.devze.com 2023-02-14 08:36 出处:网络
I have a simple query doubt. Question Table qid question 1ques1 2ques2 3ques3 4ques4 5ques5 6ques6 7ques7 Answer Table

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

精彩评论

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

关注公众号