-2
**Employee**

PK empId
firstName
lastName
isRegularEmp


**Employee_Training**

PK FK empId
PK FK trainingId
logId


**Training**

PK TrainingId
date
specialEmployeesNeeded
regularEmployeesNeeded
FK roomId

我正在尝试在特定日期之前将所有员工插入到 employee_training 表中。它的工作原理是我有一个不超过 SpecialEmployees/Regular Employees 字段值的所需金额。

例如,我在员工表中有 200 名正式员工和 100 名正式员工,但我只需要按照培训表中的规定插入 35 名特殊员工和 5 名正式员工。

我尝试了 HAVING,但它不断抛出错误未知列。请帮忙,我尝试插入一种类型的员工,但它仍然不起作用

INSERT INTO EMPLOYEE_TRAINING(empId, trainingId)


SELECT E.empId , T.TrainingId


FROM EMPLOYEE E, TRAINING T

WHERE T.`date` = "2013-4-20" 

AND E.isRegularEmp = false

HAVING COUNT(E.empId) <= regularEmployeesNeeded

我无法发布我的 erd,因为我没有足够的积分。

4

2 回答 2

1

regularEmployees不是任何表中的列,也不是在查询中的任何位置定义的。尝试

INSERT INTO EMPLOYEE_TRAINING(empId, trainingId)

SELECT E.empId , T.TrainingId

FROM EMPLOYEE E, TRAINING T

WHERE T.`date` = "2013-4-20"

AND E.isRegularEmp = false

HAVING COUNT(E.empId) <= (select count(*) from EMPLOYEE where isRegularEmp=true )
于 2013-04-11T01:41:48.053 回答
0

你的isRegularEmp条件的位置不正确。它应该放在WHERE从句中。

INSERT INTO EMPLOYEE_TRAINING(empId, trainingId)

SELECT E.empId , T.TrainingId

FROM EMPLOYEE E, TRAINING T

WHERE T.`date` = "2013-4-20"

AND E.isRegularEmp = false

HAVING COUNT(E.empId) <= regularEmployees

编辑:正如迈克尔本杰明的回答所指出的,regularEmployees不是未定义的字段。你需要正式员工的SELECTCOUNT

INSERT INTO EMPLOYEE_TRAINING(empId, trainingId)

SELECT E.empId , T.TrainingId

FROM EMPLOYEE E, TRAINING T

WHERE T.`date` = "2013-4-20"

AND E.isRegularEmp = false

HAVING COUNT(E.empId) <= (SELECT COUNT(*) FROM EMPLOYEE WHERE isRegularEmp = true)
于 2013-04-11T01:08:54.453 回答