Вопрос

i have this table

id   owner   group     model  
1    1       shapes    circle  
2    1       shapes    rectangle  
3    1       shapes    squire  
4    2       fruits    apple  
5    2       fruits    orange  
6    2       fruits    banana  
7    3       shapes    circle  
8    3       shapes    rectangle  
9    3       shapes    star

which each owner select a group and enter 3 choice, now i want to find the owner who select shapes and entered circle and star.

it must return owner id = 3

Это было полезно?

Решение

Assuming that group and model are unique for every owner

SELECT  a.owner
FROM    tableName a
WHERE   a.[group] = 'shapes' AND
        a.model IN ('circle','star')
GROUP   BY a.owner
HAVING  COUNT(*) = 2

otherwise, you need to uniquely count the number of model

SELECT  a.owner
FROM    tableName a
WHERE   a.[group] = 'shapes' AND
        a.model IN ('circle','star')
GROUP   BY a.owner
HAVING  COUNT(DISTINCT a.model) = 2

Другие советы

Within the specified parameters this will work: SELECT owner WHERE [group] = 'shapes' AND model = 'circle' INTERSECT SELECT owner WHERE [group] = 'shapes' AND model = 'star'

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top