简体   繁体   English

where 子句中的 INNER JOIN 表列“ID”不明确

[英]INNER JOIN Table Column 'ID' in where clause is ambiguous

This code created the error.此代码创建了错误。

Error Code: 1052. Column 'ID' in where clause is ambiguous错误代码:1052。 where 子句中的列“ID”不明确

      SELECT h1.name, h1.gender,h2.gender
        FROM hampster h1, hampster h2,partner p
        INNER JOIN partner on h1.ID = partner.ID1
        INNER JOIN children on h2.ID = children.ID1
WHERE h1.id = p.id1
  AND h2.id = p.id2
  AND h1.gender = h2.gender
  AND EXISTS (SELECT c.id1
            FROM children c
            WHERE c.id1 = p.id1
             OR  c.id1 = p.id2)
ORDER BY h1.gender, h1.name;

This is created when I add hampsters h2 into my query.这是在我将hampsters h2添加到我的查询中时创建的。 My goal is to use inner join to combine two instances of the hamspter table and still use 'h1' and 'h2' to refer to them.我的目标是使用内连接来组合仓鼠表的两个实例,并仍然使用“h1”和“h2”来引用它们。

This is the expected output from the query这是查询的预期输出

h1.name    h2.name gender
Alex        King    0
Grant       Alex    0
Jack        Grant   0
Cathy       Amy     1
Trish       Amy     1

You missing a table alias in where condition, server didn't know which ID column your using.您在 where 条件下缺少表别名,服务器不知道您使用的是哪个 ID 列。

Try this one :试试这个:

where h1.ID = 101其中 h1.ID = 101

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM