根據(jù)這三個語句反推其中包含的表結(jié)構(gòu):
select "gate_records"."gate_id" ,sum(gate_records.volume) as volume from "gate_records" where "gate_records"."deleted_at" is null and gate_id in(
select id from "gates" where "gates"."deleted_at" is null and "gates"."setup_land_region_id" in (
select id from "land_regions" where "land_regions"."deleted_at" is null and "land_regions"."parent_id" = 2
)
) group by "gate_records"."gate_id";
select id, name, lat_long
from "gates"
where "gates"."deleted_at" is null
and "gates"."setup_land_region_id" in (select id
from "land_regions"
where "land_regions"."deleted_at" is null
and "land_regions"."parent_id" = 2);
select gate_records.gate_id, sum(gate_records.volume) as volume, gates.name as name, gates.lat_long as lat_long
from gate_records
join gates on gate_records.gate_id = gates.id
where gate_records.deleted_at is null
and gates.deleted_at is null
and gate_id in (select id
from "gates"
where "gates"."deleted_at" is null
and "gates"."setup_land_region_id" in (select id
from "land_regions"
where "land_regions"."deleted_at" is null
and "land_regions"."parent_id" = 2))
group by gate_records.gate_id, gates.name, gates.lat_long;
這三個語句涉及了三個表:gate_records、gates 和 land_regions。
gate_records表包含字段:
gate_id
volume
deleted_at
gates表包含字段:
id
name
lat_long
deleted_at
setup_land_region_id
land_regions表包含字段:
id
parent_id
deleted_at
第一個查詢是關(guān)于gate_records表和與其相關(guān)聯(lián)的gates和land_regions表的聚合查詢。它選擇了gate_records表中的gate_id和volume字段似将,并對volume字段進(jìn)行了求和。條件是gate_records表中deleted_at為空,并且gate_id存在于滿足一定條件的gates表中狂秘。該條件是gates表中deleted_at為空,并且setup_land_region_id存在于滿足一定條件的land_regions表中(這些條件包括deleted_at為空并且parent_id為2)闰围。
第二個查詢選擇了gates表中的id赃绊、name和lat_long字段,條件是gates表中deleted_at為空羡榴,并且setup_land_region_id存在于滿足一定條件的land_regions表中(這些條件包括deleted_at為空并且parent_id為2)碧查。
第三個查詢是一個聯(lián)合查詢,涉及gate_records和gates表校仑。它選擇了gate_records表中的gate_id和volume字段的求和忠售,并將gates表中的name和lat_long字段也加入了結(jié)果集。條件包括了gate_records和gates表中的deleted_at為空迄沫,并且gate_id存在于滿足一定條件的gates表中稻扬。這些條件類似于前兩個查詢中的條件,都是關(guān)于gates表中deleted_at為空羊瘩,并且setup_land_region_id存在于滿足一定條件的land_regions表中(這些條件包括deleted_at為空并且parent_id為2)泰佳。