替代优化的子查询查询

时间:2017-11-15 04:38:52

标签: mysql sql

select * from solar_park where solar_park_uid 
in 
(select distinct solar_park from solar_park_pooling_station where pooling_station 
in
(select distinct pooling_station_uid FROM solar_inverter where solar_generator_uid in (:solarGeneratorUid));

我正在使用此查询,但由于某些原因,我想要更改此plz帮助的查询

2 个答案:

答案 0 :(得分:0)

另一种方法是将查询重写为correlated sub-query,如下所示。

select *
from solar_park sp
where exists (
                select 1 -- * can also be used
                from solar_park_pooling_station spps
                where exists (
                            select 1 -- * can also be used
                            from solar_inverter si
                            where spps.pooling_station = si.pooling_station_uid
                            and sp.solar_park_uid = spps.solar_park
                            and solar_generator_uid IN (:solarGeneratorUid)
                            )
              );

您可以查看教程: Exists and Not Exists sub-queries in MySQL

希望这会有所帮助。

答案 1 :(得分:0)

请尝试使用以下查询表格左外连接:

select * from solar_park  as SP
left outer join (
select distinct solar_park from solar_park_pooling_station
) as SPPS on SPPS.solar_park = SP.solar_park_uid
left outer join (
select distinct pooling_station_uid FROM solar_inverter where solar_generator_uid in (:solarGeneratorUid)
) as  SI on SI.pooling_station_uid= SPPS.pooling_station
where SPPS.solar_park  is not null and SI.pooling_station_uid is not null