-2
**Employee**

PK empId
firstName
lastName
isRegularEmp


**Employee_Training**

PK FK empId
PK FK trainingId
logId


**Training**

PK TrainingId
date
specialEmployeesNeeded
regularEmployeesNeeded
FK roomId

I am trying to insert all the employees into the employee_training table by a specific date. It works except that I have a required amount not to surpass which is the SpecialEmployees/ Regular Employees field value.

For instance I have 200 Regular Employees and 100 Regular Employees in the Employee Table but I only need 35 Special Employees and 5 Reegular Employees to be inserted as specified in the training table.

I tried HAVING but it keeps throwing an error unknown column. Please help, I tried inserting one type of employee , but it still doesn't work

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

I can't post my erd because I don't have enough points.

1
  • 1
    please post your attempt, and some example data would be nice... Apr 11, 2013 at 0:00

2 Answers 2

1

regularEmployees is not a column in any of your tables nor is it defined anywhere in your query. Try

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 )
1
  • Thanks for the correction, however I need to insert to the point where it does not exceed the specified value in the training table. So the query is still not working as it should
    – Marc
    Apr 11, 2013 at 2:01
0

The placement of your isRegularEmp condition is incorrect. It should be placed in the WHERE clause.

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

EDIT: As pointed out in Michael Benjamin's answer, regularEmployees is not a field is not defined. You need to SELECT the COUNT of regular employees.

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)
1
  • Sorry that was a typo . However it sill says Unknown column ' regularEmployees' in 'having clause'
    – Marc
    Apr 11, 2013 at 1:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.