我在从COALESCE
获取结果后尝试获取JOIN表SELECT
COALESCE(
(
SELECT
relocations.location_id
FROM
relocations
WHERE
parts.id=relocation.part_id
ORDER BY
relocations.date_moved
DESC
LIMIT
1
)
,
parts.location_id
) AS locationId,
locations.name
FROM
parts
JOIN
locations ON locations.id=locationId
但是查询无效,MySQL告诉我locationId是一个未知列。我怎么能解决这个问题?
表格结构
[部件] id | LOCATION_ID
[重定位] id | location_id | date_moved
[位置] id |名称
答案 0 :(得分:2)
如果我理解正确,您正尝试加入COALESCE
列。试试这个:
SELECT a.locationId, locations.name FROM
(SELECT
COALESCE(
(
SELECT
relocations.location_id
FROM
relocations
WHERE
parts.id=relocation.part_id
ORDER BY
relocations.date_moved
DESC
LIMIT
1
)
,
parts.location_id
) AS locationId
FROM
parts) a
JOIN locations ON locations.id=a.locationId