如何处理:错误代码1054。'field list'中未知列'dept'



我已经经历了:错误代码:1248。每个派生表都必须有自己的别名 找不到用于查询的解决方案,但仍然不确定它期望的正确列名是什么?

案例-1:

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 avg_sal从 (选择部门, 平均(avg_sal工资( 作为avg_sal从员工 e 分组 按部门具有部门 ("管理员","它"(( 限制 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( 从 (选择部门 , 平均(工资( 作为 avg_sal 从 员工 e 组 由 部门 有 部门 在 ("管理员", "它"(( 作为部门;

部门

@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;

第一种情况,您需要 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  ; 

并且您应该在没有过滤器的地方使用

第二种情况(不要将表别名用于与列相同的名称,例如: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; 

您可以使用 for filter 在哪里做这些值未计算但在行内容中可用的事实

相关内容

  • 没有找到相关文章

最新更新