mysql - 在 SQL 中获取 NULL 将 JOIN 作为结果

标签 mysql sql

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/

相关文章:

mysql - SQL:如何强制关系运算符考虑 NULL 值

MySQL/MariaDB 在使用 utf8mb4 时无法处理唯一键

嵌套存储过程中的 SQL 变量

mysql - 使用 Node js 和 Angular JS 以及 mysql 数据库

python - 如何在 MySQL 数据库中使用 UUID 而不是整数

mysql - SQL - 将用户定义的变量添加到表(mysql)?

SQL:计算行数直到满足条件

sql - 寻找练习来学习 SQL,使用 Northwind 数据库

php - PHP 中的长字符串加密和解密

mysql - 如何将表与其自身进行比较并在某一列中获取具有相同数据的行?