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 和描述。示例数据:1 公里、2 英里、3 码等。所以在我看来,根据 fill_thresholds 中的数字,它应该显示英里和码。现在我得到了 NULL,我不明白为什么。
最佳答案
您的最后一次加入应如下所示-
left join `units` `ptu_units` on fill_thresholds.post_threshold_unit_id = ptu_units.unit_id
现在,您正在基于两列加入单位表,第一列使用 fill_locations.unit_id =units.unit_id,第二列使用 fill_thresholds.post_threshold_unit_id =units.unit_id
,因此不会获取相应的行并显示 null .
关于mysql - 在 SQL 中获取 NULL 将 JOIN 作为结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32730221/