join - Mysql: select value that matches several criteria on multiple rows -
good evening,
i have 2 tables t1 , t2
in t1, have 2 variables, id (which uniquely identify each row) , doc (which can common several ids)
in t2, have 3 variables, id (which not uniquely identify rows here), auth , , type. each id has maximum of 1 distinct auth.
sample data:
what select docs have id auth='ep', , have id auth='us'. have additional ids other auth, have have @ least these two.
thus, have final table doc, id,and auth (there should @ least 2 ids per doc, can more if there exists additional auth , ep doc)
the desired results:
this should work:
select distinct (t1.id), t1.doc, t2.auth t1 left join t2 on t2.id = t1.id t1.doc in( select t1.doc t2 left join t1 on t1.id = t2.id t2.auth in('ep','us') group t1.doc having count(distinct t2.auth) = 2) ;
Comments
Post a Comment