1 回答
TA贡献1834条经验 获得超8个赞
我希望值在两个表中,NULL表中的值尚未分配。如果是这样:
SELECT c.pk as connected_shelf_fk,
COALESCE(i.iot_device_type_fk, c.connected_shelf) as iot_device_type_fk,
i.pk as device_fk,
FROM static.connected_shelf c JOIN
static.iot_devices i
ON c.pk = i.connected_shelf_fk
WHERE i.pk = %(device_id)s
如果该列确实不存在,您可以执行以下操作:
SELECT c.pk as connected_shelf_fk,
(SELECT iot_device_type_fk -- no alias!
FROM static.iot_devices i2
WHERE i2.pk = i.pk
) as iot_device_type_fk,
i.pk as device_fk,
FROM static.connected_shelf c JOIN
static.iot_devices i
ON c.pk = i.connected_shelf_fk
WHERE i.pk = %(device_id)s
子查询将iot_device_type_fk首先查找 iniot_devices然后 in connected_shelf。
添加回答
举报