简体   繁体   中英

How to use an inner join when using NOT Keyword

The problem is to get " Students who have not yet given exams ". In sql terms records where ims_admission.exam_status IS NOT Active .

I have three tables. ims_admission,ims_batch,ims_course respectively.

I have stored a batch_id,course_id in every student's record . These id's are the primary key of the respective tables. Using these id's i want to extract batch_name from ims_batch and course_name from ims_course table and where clause is where NOT ims_admission.exam_status ='Active'

I studied a little about joins. Made a query but i couldn't get results when using where clause. Except where clause i am getting records

This is my query

SELECT ims_admission.enq_applicant,ims_admission.batch_id,ims_admission.enq_course,ims_batch.batch_name 
FROM ims_admission 
INNER JOIN ims_batch ON ims_admission.batch_id = ims_batch.batch_id 
WHERE NOT ims_admission.exam_status = 'Active'

I want to get names of batch,course, where NOT exam_status ="Active"

Although you can do this with a LEFT JOIN , I think it is more natural to approach it using NOT EXISTS :

SELECT ia.*
FROM ims_admission ia
WHERE NOT EXISTS (SELECT 1
                  FROM ims_batch ib
                  WHERE ib.batch_id = ia.batch_id AND
                        ib.exam_status = 'Active'
                 );

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