MySQL 嵌套连接以及对非嵌套表字段的引用
这些是(示例)表:
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
我想执行以下查询:
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
不幸的是 MySQL 不喜欢嵌套连接中的外部引用:“on Clause”中的未知列“first.abc”
如果有人可以帮助我修复此类查询,我会很高兴。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
ON Parent.childid = 0 OR child.id
无效。使用ON Parent.childid in (0, child.id)
它应该如下所示:
ON parent.childid = 0 OR child.id
is invalid. UseON parent.childid in (0, child.id)
It should look like this:
请尝试以下查询:
Try this query instead: