SQL Statement Help

Results 1 to 2 of 2

Thread: SQL Statement Help

  1. #1
    Join Date
    Dec 1969

    Default SQL Statement Help

    Can anyone help me out with a little bit of SQL? Below is my SQL statment so far...<BR><BR>SELECT * FROM Students<BR>LEFT JOIN Subscription ON<BR>(Students.StudentsID = Subscription.StudentsID OR<BR>Subscription.StudentsID IS null)<BR>WHERE<BR>Subscription.CourseID = 8 OR<BR>Subscription.CourseID IS null<BR>ORDER BY Students.LastName<BR><BR>Basically what I have here is 2 tables. One that stores students and one that stores what courses they are subscribed to. It works nicely, except if a student is subscribed to more then 1 Course (and that courseID is not 8, in this case), then they wont be returned in the results. I just basically want to have every student listed and if there is no subscription for CourseID 8 (in this case) to have the subscription as NULL. Any help on this would GREATLY be appreciated!<BR>:)

  2. #2
    Join Date
    Dec 1969

    Default RE: SQL Statement Help

    I would think you could just do a <BR>SELECT *<BR>FROM Students<BR>LEFT OUTER JOIN Subscription<BR>ON Students.StudentsID = Subscription.StudentsID<BR>WHERE Subscription.CourseID = 8<BR>ORDER BY Students.LastName<BR><BR>I think the left outer join will grab everything from the table on the left (students) whether or not there is a matching record in the right table (subscription). It should put in NULLS for all of the subscription fields. Not positive, though.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts