create or replace view vw_fill_thresholds as
SELECT
fill_thresholds.id,
fill_thresholds.tenant_id,
waypoint_locations.name,
material_type.description as material,
fill_thresholds.can_size,
fill_thresholds.threshold,
units.description as unit,
fill_thresholds.post_threshold_cost as ptc,
ptu_units.description as ptu_unit
FROM fill_thresholds
left join fill_locations on fill_thresholds.fill_id = fill_locations.id
and fill_thresholds.deleted = 0
left join units on fill_locations.unit_id = units.unit_id
left join waypoint_locations on `waypoint_locations`.`id` = `fill_locations`.`waypoint_id`
left join `material_type` on `material_type`.`id` = `fill_locations`.`material`
left join `units` `ptu_units` on fill_thresholds.post_threshold_unit_id = units.unit_id
我需要选择与单位相同的列。因此,在我的fill_thresholds表中,我有两个字段,其中包含单位表中的id:示例2和3.
现在,在我的单位表中,我有以下字段:id和description。示例数据:1公里,2英里,3码等。所以在我看来,根据我在fill_thresholds中的数字,它应该显示英里和码。现在我得到NULL,我不明白为什么。
答案 0 :(得分:1)
您的查询问题在于您正在尝试猜测分析整个查询的错误。它太复杂了,所以试着逐个加入你的表。
1)fill_thresholds LEFT JOIN fill_locations
2)fill_thresholds LEFT JOIN fill_locations LEFT JOIN units
3)fill_thresholds LEFT JOIN fill_locations LEFT JOIN单位LEFT JOIN waypoint_locations 等等
这样你就可以找到问题所在
答案 1 :(得分:1)
您的上次加入应符合以下条件 -
left join `units` `ptu_units` on fill_thresholds.post_threshold_unit_id = ptu_units.unit_id
现在您首先使用fill_locations.unit_id = units.unit_id and 2nd with fill_thresholds.post_threshold_unit_id = units.unit_id
基于2列加入您的单位表,因此不会获得相应的行并显示为空。