mysql - Return result depend on conditions for multible rows without self join -
i have large answers table this
person_id|question_id|answer 1|101|6 1|102|2 1|103|5 2|101|2 2|102|5 2|103|5 3|101|2 3|102|8 3|103|6 4|101|2 4|102|8 4|103|6 4|101|6 4|102|2 4|103|5
how return persons depend on multiple question answers? example need return person answers:
6 question 101 , 2 question 102 , 5 question 103
the query should return person 1 , 4
and consider need filter depend on 10 questions, don't need 10 self join on table :)
you can using group by
, having
:
select person_id t (question_id, answer) in ( (101, 6), (102, 2), (103, 5) ) group person_id having count(distinct question_id) = 3;
note "3" needs match number of questions have in in
list.
Comments
Post a Comment