查询多个EXIST

jac*_*him 5 mysql exists

我有一个房间和设备的数据库.我想查询数据库并返回一个房间列表,例如电视,收音机,卫星和冰箱(eq1,eq2,eq3,....,eqN).

我有以下SELECT语句:

select * from rooms r where 
exists (select id from equipments where eq_id='eq1' and room_id=r.id)
and
exists (select id from equipments where eq_id='eq2' and room_id=r.id)
and
exists (select id from equipments where eq_id='eq3' and room_id=r.id)
.......
and
exists (select id from equipments where eq_id='eqN' and room_id=r.id)
Run Code Online (Sandbox Code Playgroud)

有没有什么方法可以优化或缩短它?

0xC*_*ABE 0

select * from rooms r where 
(select count(id) from equipments where eq_id='eq1' and room_id=r.id) > 0
and
...
Run Code Online (Sandbox Code Playgroud)