简体   繁体   English

将两个选择语句合并/组合成一个

[英]Merge/Combine two select statements into one

I've got the following MySQL statement that has a couple of select statements of which one does a select for all the results returned by another select.我有以下 MySQL 语句,其中有几个 select 语句,其中一个为另一个 select 返回的所有结果执行 select。 Just wondering if it can be combined into one by any chance.只是想知道它是否可以合二为一。 Eg.例如。 Like a triple join.就像三重连接。

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
);

Note.笔记。 I've done sql queries but this is my first experience with sql joins.我已经完成了 sql 查询,但这是我第一次使用 sql 连接。

Use aliases for table network_installations:为表 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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM