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

时间:2018-11-01 03:05:23

标签: mysql mysqli inner-join mysql-workbench mysql-error-1054

此代码创建了错误。

错误代码: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;

当我在查询中添加 仓鼠h2 时创建。我的目标是使用内部联接组合hamspter表的两个实例,并且仍然使用“ h1”和“ h2”来引用它们。

这是查询的预期输出

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

1 个答案:

答案 0 :(得分:-1)

您在where条件下缺少表别名, 服务器不知道您使用哪个ID列。

尝试一下:

其中h1.ID = 101