0

I have two database tables below:

Course_Module Table:

CourseId  ModuleId
1         1
1         2
1         3

Module Table:

ModuleId ModuleNo   ModuleName                            Credits
1        CHI2513    Systems Strategy                      20
2        CHI2550    Modern Database Applications          20
3        CHI2565    Ecommerce - Business and Technology   20
4        CHT2220    Interactive Systems                   20
5        CHT2520    Advanced Web Programming              20
6        CHI2554    Patent Law                            40

Now I do not know how to do this but what I want to do is create query where it will outputs a set of records which shows a list of modules which is not in the course selected.

So for example if the CourseId = 1, then the modules it should output from the query are these below:

4        CHT2220    Interactive Systems                   20
5        CHT2520    Advanced Web Programming              20
6        CHI2554    Patent Law                            40

This is because as you can see in the Course_Module table, these modules do not belong to the select CourseId 1.

But how can a query like this be written? Below is just a simple query I have created:

SELECT cm.CourseId, m.ModuleId, m.ModuleNo, m.ModuleName, m.Credits
FROM Course_Module cm
INNER JOIN Module m ON cm.ModuleId = m.ModuleId
WHERE
(CourseId = 1)
ORDER BY ModuleNo 
4

2 回答 2

2

How about the following ?

SELECT
    m.*
FROM
    Module m
WHERE
    m.ModuleId NOT IN (
                       SELECT cm.ModuleId
                           FROM Course_Module cm
                       WHERE cm.CourseId = 1
                      );
于 2012-12-01T03:09:18.870 回答
0

I think this may work:

SELECT
   Module.*
FROM
   Module
WHERE
   ModuleId NOT IN ( SELECT ModuleId FROM CourseId WHERE CourseId = 1)
于 2012-12-01T03:12:23.913 回答