简体   繁体   English

从多个表中选择时,MySQL JOIN 会导致“错误 1054 (42S22):未知列”

[英]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.

相关问题 MYSQL:ERROR 1054(42S22):'from子句'中的未知列'id_employee' - MYSQL: ERROR 1054 (42S22): Unknown column 'id_employee' in 'from clause' ERROR 1054(42S22):未知列 - ERROR 1054 (42S22): Unknown column 错误1054(42S22):“ where子句”中的未知列“ - ERROR 1054 (42S22): Unknown column '' in 'where clause' ERROR 1054(42S22):'字段列表'中的未知列'' - ERROR 1054 (42S22): Unknown column '‍‍' in 'field list' (MySQL)SQL错误:错误1054(42S22):未知列&#39; <Column Name> 在“字段列表”中 - (MySQL) SQL Error : ERROR 1054 (42S22): Unknown column '<Column Name>' in 'field list' MySQL错误1054(42S22)“ where子句”中的未知列检查该列是否存在 - Mysql ERROR 1054 (42S22) Unknown column in 'where clause' Checking if column exists 错误1054(42S22):MySQL中“字段列表”中的未知列“员工” - ERROR 1054 (42S22): Unknown column 'employee' in 'field list' in MySQL 关于“字段列表”中不存在未知列的MYSQL错误1054(42S22)投诉 - MYSQL ERROR 1054 (42S22) complaint about Unknown column in 'field list' that does not exist MySQL 错误消息:“1054 (42S22):‘字段列表’中的未知列‘inf’” - MySQL error message: “1054 (42S22): Unknown column 'inf' in 'field list'” MySQL:错误1054(42S22):“ where子句”中的未知列 - MySQL: ERROR 1054 (42S22): Unknown column in 'where clause'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM