“ on子句”中的未知列“ b.Hostname”

时间:2019-07-11 15:09:20

标签: mysql sql oracle ansi

我有以下MYSQL查询-试图联接3个表并查找唯一的信息(数据)

SELECT a.LocationID, a.Model, a.SerialNum,a.Purpose, b.IP, a.Services,a.DeviceID, COUNT(a.Hostname)
FROM RefConnection.Equipment_Info a, RefConnection.Connections b, RefConnection.VM_Info c
JOIN Equipment_Info on b.Hostname = a.Hostname
WHERE a.Hostname = c.Hostname
  AND b.status = a.Status
  AND a.status = c.Status
GROUP BY a.LocationID, a.Model, a.SerialNum, a.Purpose, b.IP, a.Services, a.DeviceID
ORDER BY COUNT(b.Hostname)DESC;

这适用于2个表:

SELECT d.locationID, d.Model, d.SerialNum, d.Status, da.IP, COUNT(d.HOSTNAME)
FROM RefConnection.Equipment_Info d, RefConnection.Connections da
WHERE d.Hostname = da.Hostname
  AND d.Status = da.Status
Group By d.locationID, d.Model, d.locationID, d.Model, d.SerialNum, d.Status, da.IP
ORDER BY COUNT(da.Hostname) DESC;

“ on子句”中的未知列“ b.Hostname”

1 个答案:

答案 0 :(得分:1)

从不FROM子句中使用逗号。 始终使用正确的,明确的,标准 JOIN语法。

FROM RefConnection.Equipment_Info a JOIN
     RefConnection.Connections b
     ON b.Hostname = a.Hostname AND
        b.status = a.Status JOIN
     RefConnection.VM_Info c
     ON a.Hostname = c.Hostname AND
        a.status = c.Status

您的问题是标识符的范围规则。这与您期望的逗号不一样-避免它们的另一个原因。

我不知道Equipment_Info在查询中第二次在做什么。

我也强烈建议您使用有意义的表别名,例如eci,而不要使用任意字符。