将两个select语句合并/合并为一个

时间:2015-11-22 19:37:22

标签: mysql sql join

我有以下MySQL语句,其中包含几个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 join。

1 个答案:

答案 0 :(得分:1)

为表network_installations使用别名:

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