select query using joins and subquery.

S

student

tblQuestion A


qId(pk) Question catId
10 Which book is this? 20
11 Who is this person? 20
12 Where were you born? 20
13 How to build a fence? 25
14 Why did he say that? 20
15 Who is her role model? 20



tblSessionQuessions B
SqId(pk) sessionId qId_Correct qId_Incorrect

40 1 10
41 1 11
42 1 12
43 1 15
44 3 15

where the 2 tables are using referential integrity on
B.qId_Correct and B.qid _Incorrect on A.qId

I wan't to select questions from tblQuestion which belong
to a specific category (e.g. catId=20) and (for a
particular session (e.g. sessionId =1) that question is
not there in qId_Correct. So questions with qId 11,12,14
should be selected.
How do I accomplish this?
 
T

Tom Ellison

Dear Student:

SELECT A.*
FROM tblQuestion A
LEFT JOIN (SELECT * tblSessionQuessions WHERE SessionId = 1) B
ON B.qld_Correct = A.qld
WHERE A.catID = 20
AND B.qld_Correct IS NULL

Probably the biggest "trick" is to be sure to filter the SessionId out
using the subquery inside the join.

tblQuestion A


qId(pk) Question catId
10 Which book is this? 20
11 Who is this person? 20
12 Where were you born? 20
13 How to build a fence? 25
14 Why did he say that? 20
15 Who is her role model? 20



tblSessionQuessions B
SqId(pk) sessionId qId_Correct qId_Incorrect

40 1 10
41 1 11
42 1 12
43 1 15
44 3 15

where the 2 tables are using referential integrity on
B.qId_Correct and B.qid _Incorrect on A.qId

I wan't to select questions from tblQuestion which belong
to a specific category (e.g. catId=20) and (for a
particular session (e.g. sessionId =1) that question is
not there in qId_Correct. So questions with qId 11,12,14
should be selected.
How do I accomplish this?

Tom Ellison
Ellison Enterprises - Your One Stop IT Experts
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top