简体   繁体   中英

SQL Query in c#

I might have a problem with my SQL query. In this query I'm combining 4 different tables.

  • I have a table courses where general information is stored ( course_number , course_title ).
  • I have a table employees where general information of employees isstored ( empname , and a job_id ).
  • A employee has a job. A employee needs to take courses. It depends on the job which courses he has to take. This info is stored in the table job_course (with the job_id and the course_id ).
  • If a employee completed a course it is stored in the table emp_courses (with the e_id and the course_id )

Now I want to search a certain course - when the user presses the search button he should get two different results.

  • The first one: here you can see which employee already took this course (this query works so far)
  • the second one: here you can see which employee still needs to take the course . So i need to check which job the employee has and if he needs to make that course . and also i just want to have the ones that are not completed yet. And that's the query that is not working

Here it is:

OpenDb_Open("select course_number,course_title, empname from course 
INNER JOIN (job_course INNER JOIN (employee INNER JOIN emp_course  
ON emp_course.e_id<>employee.e_id) ON job_course.job_id=employee.job_id) 
ON course.course_id=job_course.course_id 
where course_number like '" + coursenumber + "'");

Can someone please help me with this?

Courses the employee hasn't taken.

SELECT * FROM courses
WHERE course_number IN (
    SELECT course_id FROM job_course
    WHERE course_id NOT IN (
        SELECT course_id FROM emp_courses
        WHERE emp_id = {someid}
    ) AND job_id = (
        SELECT job_id FROM employees
        WHERE emp_id = {user_input}
    )
)

Which employees still need to take a course.

SELECT emp_name FROM employees
WHERE emp_id NOT IN (
    SELECT emp_id FROM emp_courses
    WHERE course_id = {user_input}
)

Variant of above.

SELECT emp_name FROM employees
WHERE emp_id NOT IN (
    SELECT emp_id FROM emp_courses
    WHERE course_id = (
        SELECT course_id FROM courses
        WHERE course_number = {user_input}
    )
)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM