sql - MySQL : Getting Entries from a many-to-many table -
i have following table, select records should have mentioned values. example select companyid should have 3 providerid 200,300,400. running query should retrun companyid 2. opposite of in. in advance !
companyid | providerid -------------------------------------- 1 100 2 200 3 500 4 600 2 300 2 400 7 100
do group by, select rows of 3 wanted employeeid values. use having make sure 3 different employeeid values.
select companyid tablename employeeid in (200, 300, 400) group companyid having count(distinct employeeid) = 3 another approach double self join, 1 each employeeid. however, less flexible:
select distinct t1.companyid (select companyid tablename employeeid = 200) t1 join (select companyid tablename employeeid = 300) t2 on t1.companyid = t2.companyid join (select companyid tablename employeeid = 400) t3 on t2.companyid = t3.companyid
Comments
Post a Comment