多表连接查询问题
select
p.id,p.full_name,p.short_name,p.py_code,p.building_type,d.name
from
tb_project p,tb_dict_entity d
where d.`code`=p.building_type
and d.type_code='PROJECT_BUILDING_TYPE'
and p.id = 257
tb_project: 项目表;
tb_dict_entity: 字典表。
项目表中的building_type
字段对应字典表的code
字段,但building_type
并不是必须的。
这就造成,如果building_type
字段有值,那么可以查出数据。
但如果building_type
字段没有值,是查不出数据的,因为字典表中没有为null的code
.
换成左外连接也不行
这样就可以了:
select
p.id,p.full_name,p.short_name,p.py_code,p.building_type,d.name
from
tb_project p left join tb_dict_entity d on d.`code`=p.building_type and d.type_code='PROJECT_BUILDING_TYPE'
where p.id = 258
多表左外连接也是一样的道理:
select
p.id,p.full_name,p.short_name,p.py_code,p.building_type,d.name as building_type_str,p.climate_type,d2.name as climate_type_str
from
tb_project p left join tb_dict_entity d on d.`code`=p.building_type and d.type_code='PROJECT_BUILDING_TYPE'
left join tb_dict_entity d2 on d2.`code`=p.climate_type and d2.type_code='SYS_ClimateArea'
where p.id=258