Is it possible to extract information based on more than one condition in a single query
Answers
Answered by
2
You can either use GROUP BY and HAVING COUNT(*) = _:
SELECT contact_id FROM your_table WHERE flag IN ('Volunteer', 'Uploaded', ...) GROUP BY contact_id HAVING COUNT(*) = 2 -- // must match number in the WHERE flag IN (...) list(assuming contact_id, flag is unique).
Or use joins:
SELECT T1.contact_id FROM your_table T1 JOIN your_table T2 ON T1.contact_id = T2.contact_id AND T2.flag = 'Uploaded' -- // more joins if necessary WHERE T1.flag = 'Volunteer'If the list of flags is very long and there are lots of matches the first is probably faster. If the list of flags is short and there are few matches, you will probably find that the second is faster. If performance is a concern try testing both on your data to see which works best.
Similar questions
Social Sciences,
8 months ago
Math,
8 months ago
Accountancy,
8 months ago
Physics,
1 year ago
Science,
1 year ago
Math,
1 year ago
English,
1 year ago