sql - Select only if related entities column contains specific value -
i having hard time figuring out sql query. want pick companies has licenses name containing "test".
company license |pkid|name | |pkid|companyid|name | ----------------- ----------------------------- |1 |microsoft | |1 |1 |license test| |2 |apple | |2 |1 |commercial | |3 |2 |license test| |4 |2 |license test|
so, in example microsoft has 2 licenses. 1 test , 1 commercial don't want company. of apples licenses test licenses want select apple.
what i'm thinking of is:
select company.name, count(company.name) company inner join license on license.companyid = company.pkid license.name '%test%' group company.name
to how many rows containing "test" each company , compare
select company.name, count(company.name) company inner join license on license.companyid = company.pkid group company.name
and if there no difference in count, have company test licenses. have no idea how or if there better way.
what want subtract 1 set (that is, return 1 set, without rows appear in set), , except (or minus in oracle) operator does. query should give want:
select c.pkid, c.name company c inner join license l on c.pkid=l.companyid l.name '%test%' except select c.pkid, c.name company c inner join license l on c.pkid=l.companyid l.name not '%test%'
there other ways of doing this, should straightforward.
Comments
Post a Comment