一个多对多的表的查询优化
r表 b表为中间表 c表
select
r.*
from r
where 1=1
and 3 in (SELECT b.cid FROM b where r.id = b.rid)
and 2 in (SELECT b.cid FROM b where r.id = b.rid)
怎么对(SELECT b.cid FROM b where r.id = b.rid)这个语句优化那
如果取并集的话,下面这条语句就解决了,但是我想要的是交集
select
r.*
from r
where 1=1
and r.id in(select distinct r.id from r,b where b.rid=r.id and b.cid in(2,3));