How to Retrieve Data Using "OR" Operator
I have a question that I need your assistance.
Let's say if I want to find if the people in a certain district (districtID=5) has taken Course A (cID=11 or cID=12) BUT not both since they have the same course name with different cID, how do you write this in SQL, along with their FirstName, LastName and Email? Can you kindly help? Here are the fields:
peopleID FirstName LastName Email schoolIDNum
1 Esther B b@hotmail.com 33
2 Tommy L l@hotmail.com 55
registrationID1 peopleIDNum sectionIDNum
22 1 40
23 2 40
sectionID courseIDNum districtIDNum
40 11 5
41 12 5
schoolID districtIDNum
33 5
55 5
My codes below didn't work. It keeps giving me 0 rows. Any help or advice is appreciated.
SELECT p.FirstName, p.LastName, p.Email, s.CourseIDNum
FROM People p, Registration r, Section s, School sc
WHERE p.PeopleID=r.PeopleIDNum AND r.SectionIDNum=s.SectionID AND
(s.CourseIDNum=11 OR s.CourseIDNum=12) AND sc.SchoolID=p.SchoolIDNum AND
s.DistrictIDNum=2 AND r.Completed='Y'
I have a question that I need your assistance.
Let's say if I want to find if the people in a certain district (districtID=5) has taken Course A (cID=11 or cID=12) BUT not both since they have the same course name with different cID, how do you write this in SQL, along with their FirstName, LastName and Email? Can you kindly help? Here are the fields:
peopleID FirstName LastName Email schoolIDNum
1 Esther B b@hotmail.com 33
2 Tommy L l@hotmail.com 55
registrationID1 peopleIDNum sectionIDNum
22 1 40
23 2 40
sectionID courseIDNum districtIDNum
40 11 5
41 12 5
schoolID districtIDNum
33 5
55 5
My codes below didn't work. It keeps giving me 0 rows. Any help or advice is appreciated.
SELECT p.FirstName, p.LastName, p.Email, s.CourseIDNum
FROM People p, Registration r, Section s, School sc
WHERE p.PeopleID=r.PeopleIDNum AND r.SectionIDNum=s.SectionID AND
(s.CourseIDNum=11 OR s.CourseIDNum=12) AND sc.SchoolID=p.SchoolIDNum AND
s.DistrictIDNum=2 AND r.Completed='Y'
No comments:
Post a Comment