简体   繁体   中英

finding the courses not taking in Staff table ? using SQL

I have two tables The First one contains information about staff who take courses 第一个表名为Staff

The second table information about all courses available and it must taking by staff 第二张表命名为Course

I want quereu SQL to find the remaining course for every staff

for example :-

Rami intro. Java

Rami into. to Orcad

Rami PIC

Rami intro. to C++

Rayan Intro. Flash

Rayan Intro to c++

Rayan PIC

Rayan Intro. to Photoshop

Rayan Into. to Orcad

.

.

.

. . . . . and so on for all staff

Didn't test! But it might work:

SELECT staff.Name, courses.CourseName 
FROM courses LEFT JOIN staff
             ON (staff.TrainingCourse = courses.CourseName)
WHERE TrainingCourse IS NULL

I'll assume you have a Staff table with 1 record per staff member, and a courses table with 1 record per course. And the first table in your post I'll call the CourseRegistrations table.

SELECT Staff.Name, Courses.CourseName 
FROM Courses CROSS JOIN Staff
WHERE NOT EXISTS (SELECT * 
                  FROM CourseRegistrations 
                  WHERE Name = Staff.Name 
                  AND TrainingCourseName = Courses.CourseName)

If you're not familiar with CROSS JOIN's what this is doing is finding all possible combinations of staff and courses, then in the WHERE clause it filters out all combination that already exist in your registration table.

With these 3 tables ...

Courses:

CourseName
intro. Flash
intro. Java
intro. to Photoshop
into. To Orcad
intro. to c++
PIC

Staff:

sname
Khalid
Rami
Rayan

StaffCourses:

sname   Trainingcourse
Khalid  into. To Orcad
Rami    intro. Flash
Rami    intro. to Photoshop
Rayan   intro. Java

... this query ...

SELECT
    xjoin.sname,
    xjoin.CourseName
FROM
    [
        SELECT Courses.CourseName, Staff.sname FROM Courses, Staff
    ]. AS xjoin
    LEFT JOIN StaffCourses
    ON
            (xjoin.CourseName = StaffCourses.Trainingcourse)
        AND (xjoin.sname = StaffCourses.sname)
WHERE
    (((StaffCourses.Trainingcourse) Is Null))
ORDER BY
    xjoin.sname,
    xjoin.CourseName;

... produces this result set:

sname  CourseName
Khalid intro. Flash
Khalid intro. Java
Khalid intro. to c++
Khalid intro. to Photoshop
Khalid PIC
Rami   into. To Orcad
Rami   intro. Java
Rami   intro. to c++
Rami   PIC
Rayan  into. To Orcad
Rayan  intro. Flash
Rayan  intro. to c++
Rayan  intro. to Photoshop
Rayan  PIC

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