【问题标题】:ERROR 1054 (42S22): Unknown column 'employee' in 'field list' in MySQL错误 1054 (42S22): MySQL 中“字段列表”中的未知列“员工”
【发布时间】:2018-07-20 05:06:46
【问题描述】:

我不确定为什么会收到此错误消息 ERROR 1054 (42S22): Unknown column 'employee' in 'field list'。

Employee Table Description

Employee Table

Work Table Description

Work Table

这是员工表的数据类型和插入的值:

mysql> create table employee (`employee-name` char(30), `street` char(30), 
`city` char(30));
mysql> insert into employee values ('John Smith', 'Street A', 'City 1');
mysql> insert into employee values ('Arya Stark', 'Street B', 'City 2');
mysql> insert into employee values ('Barry Allen', 'Street C', 'City 3');
mysql> insert into employee values ('Wanda Maximoff', 'Street D', 'City 4');
mysql> insert into employee values ('Raven Roth', 'Street E', 'City 5');

这是工作表的数据类型和插入的值:

mysql> create table works (`employee-name` char(30), `company-name` char(50), 
`salary` decimal(20,2));
mysql> insert into works values ('John Smith', 'MyBank', '10000');
mysql> insert into works values ('Arya Stark', 'First Bank Corporation', 
'20000');
mysql> insert into works values ('Barry Allen', 'MyBank', '17000');
mysql> insert into works values ('Wanda Maximoff', 'YourBank', '125000');
mysql> insert into works values ('Raven Roth', 'MyBank', '20000');

我从实验室实践中得到的问题是: 查找姓名、街道地址、 和所有在“第一银行”工作的员工的居住城市 公司”并赚取超过 10,000 美元

每次我输入这个错误都会出现:

select employee.employee-name, employee.street, employee.city from
employee, works
where employee.employee-name=works.employee-name
and company-name = 'First Bank Corporation' and salary > 10000;
ERROR 1054 (42S22): Unknown column 'employee.employee' in 'field list'

【问题讨论】:

  • 从员工中减去姓名似乎是一件奇怪的事情
  • 避免使用特殊字符,而是使用驼峰式大小写。

标签: mysql


【解决方案1】:

您需要转义包含 - 等特殊字符的表/列名称

select employee.`employee-name`...

此外,不要再使用传统的连接样式了。它在 20 年前被显式连接所取代。

select e.`employee-name`, e.street, e.city
from employee e
join works w on e.`employee-name` = w.`employee-name`
where `company-name` = 'First Bank Corporation' 
and salary > 10000

【讨论】:

  • 我会走得更远。在表/列标识符中包含“-”是一场等待发生的灾难。
猜你喜欢
  • 2018-05-31
  • 2018-03-31
  • 1970-01-01
  • 2022-06-17
  • 2015-11-16
  • 2017-03-17
  • 2014-08-04
  • 2013-10-13
  • 1970-01-01
相关资源
最近更新 更多