错误代码:1054。“字段列表”中的未知列“部门”0.000 秒
Posted
技术标签:
【中文标题】错误代码:1054。“字段列表”中的未知列“部门”0.000 秒【英文标题】:Error Code: 1054. Unknown column 'dept' in 'field list' 0.000 sec 【发布时间】:2019-08-16 06:38:09 【问题描述】:我已经通过了:Error Code: 1248. Every derived table must have its own alias No solution found for query,但仍然不确定它期望的正确列名是什么?
案例一:
SELECT max(avg_sal), min(avg_sal)
FROM (SELECT dept , avg(salary) as avg_sal from Employee e GROUP BY dept HAVING dept in ('Admin','IT'));
12:04:44 SELECT max(avg_sal), min(avg_sal) FROM (SELECT dept , avg(salary) as avg_sal from Employee e GROUP BY Dept HAVING dept in ('Admin','IT')) LIMIT 0 , 1000 错误代码: 1248. 每个派生表都必须有自己的别名 0.000 秒
案例:2
SELECT max(avg_sal), min(avg_sal)
FROM (SELECT dept , avg(salary) as avg_sal from Employee e GROUP BY dept HAVING dept in ('Admin','IT')) as dept;
然后我得到
选择最大值(avg_sal),最小值(avg_sal) FROM (SELECT dept , avg(salary) as avg_sal from Employee e GROUP BY dept HAVING dept in ('Admin','IT')) as dept;
部门
@Entity
public class Dept
@Id
@GeneratedValue
private Integer id;
private String name;
员工
@Entity
@NamedQuery(name = "Employee.findMaxSalariesByDept",
query = "SELECT e.dept, MAX(e.salary) FROM Employee e GROUP BY e.dept.name HAVING e.dept.name in ?1")
public class Employee
@Id
@GeneratedValue
private Long id;
private String name;
@ManyToOne(cascade = CascadeType.ALL)
private Dept dept;
private int salary;
显示表格:
CREATE TABLE `employee` (
`id` bigint(20) NOT NULL AUTO_INCREMENT,
`name` varchar(255) DEFAULT NULL,
`salary` int(11) NOT NULL,
`dept_id` int(11) DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `FKfow2bhgypdy2ij4oyukrn6cqw` (`dept_id`)
) ENGINE=MyISAM AUTO_INCREMENT=6 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;
【问题讨论】:
您能否将SHOW CREATE TABLE Employee;
的输出添加到您的问题中?这个表好像缺少dept
这列
@digijay - 添加了询问的详细信息
正如@scaisEdge 在下面提到的,您没有dept
列,而只有dept_id
。您可以尝试在 Case-1 查询中更改它吗?
【参考方案1】:
第一种情况,您需要 FROM() 子句的表别名,例如:FROM ( ) T
SELECT max(T.avg_sal)
, min(T.avg_sal)
FROM (
SELECT dept_id dept, avg(salary) as avg_sal
from Employee e
GROUP BY dept_id
WHERE dept_id in ('Admin','IT')
) T ;
你应该使用 where 没有过滤器
第二种情况(表别名不要使用与列相同的名称 eg:dept 但使用 t_dept)
SELECT max(t_dept.avg_sal), min(t_dept.avg_sal)
FROM (
SELECT dept_id dept, avg(salary ) as avg_sal
from Employee e
WHERE dept_id in ('Admin','IT')
GROUP BY dept
) as t_dept;
并且您可以使用 where for filter 来执行这些值未计算但在行内容中可用的事实
【讨论】:
我仍然收到同样的错误:13:25:21SELECT max(t_dept.avg_sal), min(t_dept.avg_sal) FROM ( SELECT dept , avg(salary ) as avg_sal from Employee e WHERE dept in ('Admin','IT') GROUP BY dept ) as t_dept LIMIT 0
,1000 错误代码:1054。“字段列表”中的未知列“部门”0.000 秒
您的表中没有 dept 列(您有 dept_id).. 使用正确的列名(和列别名)更新答案以上是关于错误代码:1054。“字段列表”中的未知列“部门”0.000 秒的主要内容,如果未能解决你的问题,请参考以下文章
错误代码:1054。存储过程中“字段列表”中的未知列“abcdef”
找不到列:1054“字段列表”中的未知列“0”-Laravel-我的代码中的任何地方都没有0列
#1054 - '字段列表'中的未知列 'id' - phpMyAdmin