These are the (example) tables:
first: id abc
parent: id childid foo
1 0 -- no child
2 1 -- one child
child: id abc bar
1
2
last: id foo bar
I would like to execute the following query:
SELECT
first.*
,parent.*
,child.*
,last.*
FROM first -- actually some joins
-- select exactly 1 `parent` row for each `first` row
-- with an optional `child` row (or NULLs if child.id=0)
LEFT JOIN ( parent
LEFT JOIN child ON child.id = parent.childid
AND child.abc = first.abc <== ERROR HERE
) ON parent.childid = 0
OR child.id
-- conditions referring to the parent,child rows
LEFT JOIN last ON last.foo = parent.foo
AND last.bar = child.bar
Unfortunately MySQL doesn’t like external references in the nested join:
Unknown column 'first.abc' in 'on clause'
I would be happy if someone could help me on fixing this type of query.
ON parent.childid = 0 OR child.idis invalid. UseON parent.childid in (0, child.id)It should look like this: