I am trying to get JOIN a table after I get a result from a SELECT with COALESCE
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
However the query is not valid and MySQL tells me locationId is an Unknown column. How could I fix this?
Table structure
[Parts]
id | location_id
[Relocations]
id | location_id | date_moved
[Locations]
id | name
If I understand correctly, you are trying to join on the
COALESCEcolumn. Try this: