i have 2 table question , answer tables. question table has
id question_text 1 question1 2 question2 3 question3
answer table
question_id answer_text answered_by 1 ans1 user1 1 ans2 user2 1 ans3 user3 2 ans4 user1
i need find out questions answered already? desired outcome
id question_text answered 1 question1 true/1 2 question2 true/1 3 question3 false/0
i have written below query find whether question answered using exists. need answered value true/false , want fine tune query checks first occurrence in answer table , value true/false, instead of running through thousands of value in answer table.
select * questions exists (select * answers b a.question_id = b.question_id);
a simple left join
do:
select q.*, case when a.question_id null 0 else 1 end answered question q left join ( select distinct question_id answer) on q.id = a.question_id;
Comments
Post a Comment