下面的MySQL语句有两个select语句,其中一个对另一个select返回的所有结果执行select。只是想知道它是否有可能合并成一个。就像三重连接。
select networks.name as network_name, installations.name as installation_name
from network_installations
join installations on installations.id = network_installations.installation_id
join networks on networks.id = network_installations.network_id
where network_installations.network_id in (
select network_installations.network_id
from network_installations
where network_installations.installation_id = 1
);
注意。我做过sql查询,但这是我第一次使用sql连接。
最佳答案
为表网络安装使用别名:
select networks.name as network_name, installations.name as installation_name
from network_installations ni1
join installations on installations.id = ni1.installation_id
join networks on networks.id = ni1.network_id
join network_installations ni2 on ni2.network_id = ni1.network_id
where ni2.installation_id = 1