[英]MySQL JOIN causes "ERROR 1054 (42S22): Unknown column" when selecting FROM multiple tables
Given the following schema:鉴于以下架构:
CREATE TABLE Customers
(
CustomerID INT,
FirstName VARCHAR(32),
LastName VARCHAR(32)
);
CREATE TABLE Programs
(
ProgramID INT,
ProgramName VARCHAR(32)
);
CREATE TABLE Registrations
(
RegistrationID INT,
ProgramID INT,
CustomerID INT
FirstName VARCHAR(32),
LastName VARCHAR(32)
);
I can't think of any reason why this query should fail:我想不出这个查询失败的任何原因:
mysql> SELECT *
FROM Registrations R, Programs P
LEFT OUTER JOIN Customers C
ON R.CustomerID = C.CustomerID
WHERE R.ProgramID = P.ProgramID;
ERROR 1054 (42S22): Unknown column 'C.CustomerID' in 'on clause'
But running the query by selecting only the one table Registrations
causes no errors:但是通过仅选择一个表
Registrations
来运行查询不会导致错误:
mysql> SELECT *
FROM Registrations R
LEFT OUTER JOIN Customers C
ON R.CustomerID = C.CustomerID;
Empty set (0.00 sec)
What am I missing?我错过了什么?
Before you suggest adding Customers
to FROM, CustomerID
is a "loose" association.在您建议将
Customers
添加到 FROM 之前, CustomerID
是一个“松散”关联。 For reasons not relevant to this problem, customer data sometimes needs to be decoupled from the registration data with a NULL value for CustomerID.由于与此问题无关的原因,有时需要使用 CustomerID 的 NULL 值将客户数据与注册数据分离。
the problem is related to the use of a mix of implicit and explicit join该问题与隐式和显式连接的混合使用有关
You can't use implict join based on list of comma separated table name and where clause and explicit join in the same query ..您不能在同一查询中使用基于逗号分隔的表名列表和 where 子句和显式连接的隐式连接..
SELECT *
FROM Registrations R
INNER JOIN Programs P ON R.ProgramID = P.ProgramID
LEFT OUTER JOIN Customers C ON R.CustomerID = C.CustomerID
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.