SQL query for parent with specific values in child set
问题 I am trying to return just the parent where the set of children contain multiple specific records. Given this table: Product State ------- ----- 111 AZ 111 CA 111 UT 222 AZ 222 WA 333 CA I want find the list of Product that have both a AZ child record and a CA child record, even if it also has other records. Which would return ... Product ------- 111 回答1: select product from table where state in ('az','ca') group by product having count(distinct(state)) = 2 select distinct product from table