MySQL 嵌套连接以及对非嵌套表字段的引用

发布于 2024-12-09 22:26:35 字数 921 浏览 0 评论 0原文

这些是(示例)表:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

你是暖光i 2024-12-16 22:26:35
  1. 失去那些括号!
  2. ON Parent.childid = 0 OR child.id 无效。使用 ON Parent.childid in (0, child.id)

它应该如下所示:

...
LEFT JOIN  parent  -- removed opening bracket
LEFT JOIN   child ON child.id = parent.childid
                 AND child.abc = first.abc 
                 AND parent.childid = in (0, child.Id) -- removed closing bracket and used 'IN'
...   
  1. Lose those brackets!
  2. ON parent.childid = 0 OR child.id is invalid. Use ON parent.childid in (0, child.id)

It should look like this:

...
LEFT JOIN  parent  -- removed opening bracket
LEFT JOIN   child ON child.id = parent.childid
                 AND child.abc = first.abc 
                 AND parent.childid = in (0, child.Id) -- removed closing bracket and used 'IN'
...   
屋顶上的小猫咪 2024-12-16 22:26:35

请尝试以下查询:

SELECT first.*, parent.*, child.*, last.*
FROM first
LEFT JOIN parent ON parent.childid = 0 OR parent.childid IS NOT NULL
LEFT JOIN child ON child.id = parent.childid AND child.abc = first.abc
LEFT JOIN last  ON last.foo = parent.foo AND last.bar = child.bar

Try this query instead:

SELECT first.*, parent.*, child.*, last.*
FROM first
LEFT JOIN parent ON parent.childid = 0 OR parent.childid IS NOT NULL
LEFT JOIN child ON child.id = parent.childid AND child.abc = first.abc
LEFT JOIN last  ON last.foo = parent.foo AND last.bar = child.bar
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文