簡體   English   中英

mysql select語句不返回未填充查詢中某些表的行

[英]mysql select statement not returning rows where some tables in query aren't populated

我腦子里一片空白-這段代碼很好地選擇了行,除了我將st.station_id從值'1'更改為另一個(但仍然有效)的數字,但沒有該station_id的條目在station_owner_map表,組織表或cap_gen_data_table中。 我基本上需要修改我的sql,以忽略沒有條目的任何表。

Select st.station_id, st.station_name , st.st_town, st.st_state, c1.country_name,   o1.organisation_name, som1.equity, st.river_basin, st.cost, st.cost_ref, st.comm_year,cg1.caporgen, ht1.hydro_name, cg1.value, srs1.srs_description, cg1.ref_year
FROM station st
inner join station_country_map scm1 on st.station_id = scm1.station_id
inner join country c1 on scm1.country_id = c1.country_id
inner join station_owner_map som1 on st.station_id = som1.station_id
inner join organisation o1 on som1.owner_id = o1.org_id
inner join cap_gen_data cg1 on st.station_id = cg1.station_id
inner join value_lookup vl1 on cg1.caporgen = vl1.id
inner join hydro_type ht1 on cg1.hydro_type_id = ht1.type_id
inner join station_record_status srs1 on cg1.capacity_status = srs1.st_rec_stat_id
where st.station_id = 1

這是由您的inner join引起的。 Inner join意味着兩個表中都必須有一個值,記錄才能顯示在結果集中。

請改用left join ,然后僅“左”表必須有一個值。

在可能不存在該值的表上使用left聯接。

如果您有兩個表AB ,則內部聯接將僅返回滿足聯接條件的A中的行。 left聯接將返回A所有行,而不管聯接條件是否滿足。 當使用左連接時,與B關聯的select語句中的列將為null。

我僅將left join添加到您指示的表中。 如果其他表可能不滿足聯接條件,則將聯接類型從inner更改為left

Select st.station_id, st.station_name , st.st_town, st.st_state, c1.country_name,   o1.organisation_name, som1.equity, st.river_basin, st.cost, st.cost_ref, st.comm_year,cg1.caporgen, ht1.hydro_name, cg1.value, srs1.srs_description, cg1.ref_year
FROM station st
inner join station_country_map scm1 on st.station_id = scm1.station_id
inner join country c1 on scm1.country_id = c1.country_id
left join station_owner_map som1 on st.station_id = som1.station_id
left join organisation o1 on som1.owner_id = o1.org_id
left join cap_gen_data cg1 on st.station_id = cg1.station_id
inner join value_lookup vl1 on cg1.caporgen = vl1.id
inner join hydro_type ht1 on cg1.hydro_type_id = ht1.type_id
inner join station_record_status srs1 on cg1.capacity_status = srs1.st_rec_stat_id
where st.station_id = 1

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM