简体   繁体   English

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

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

I'm having a real mind blank - This code selects rows quite nicely apart from those entries where I change st.station_id from a value of '1' to a different (but still valid) number but where there are no entries for that station_id in either the station_owner_map table, the organisation table or the cap_gen_data_table. 我脑子里一片空白-这段代码很好地选择了行,除了我将st.station_id从值'1'更改为另一个(但仍然有效)的数字,但没有该station_id的条目在station_owner_map表,组织表或cap_gen_data_table中。 I basically need to amend my sql to ignore any table where there are no entries. 我基本上需要修改我的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

It's caused by your inner join s. 这是由您的inner join引起的。 Inner join means there has to be a value in both tables for the record to show up in the result set. Inner join意味着两个表中都必须有一个值,记录才能显示在结果集中。

Use left join instead, then only the table 'on the left' has to have a value. 请改用left join ,然后仅“左”表必须有一个值。

Use left join on tables where the value may not be present. 在可能不存在该值的表上使用left联接。

If you have two tables A and B an inner join will only return the rows from A where the join condition is met. 如果您有两个表AB ,则内部联接将仅返回满足联接条件的A中的行。 A left join will return all rows from A regardless of if the join condition is satisfied. left联接将返回A所有行,而不管联接条件是否满足。 Columns in the select statement associated with B will be null when a left join is used. 当使用左连接时,与B关联的select语句中的列将为null。

I have only added the left join to the tables you have indicated. 我仅将left join添加到您指示的表中。 If other tables may not satisfy the join condition change the join type from inner to left . 如果其他表可能不满足联接条件,则将联接类型从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