junkyjunk
junkyjunk

Reputation: 3

Having trouble with SQL command logic - please help

I am using the following statement to fill an Excel spreadsheet with student information, including "actualstudenthours."

The problem is, I want to show all students for which the tblstudentstatus.id = 3, but I also need to show actual student hours for those students. Unfortunately, not all of the students have a corresponding entry in "viewactualstudenthours." This statement completely leaves out those students for which there is no corresponding entry in "viewatualstudenthours."

How do I get all the students to show up where the tblstudentstatus.id = 3?

If there is no entry for them in viewactualstudenthours, it should not omit the student entirely...the student hours fields should just be blank. Your help would be greatly appreciated.

$result=mysqli_query($dbc,"SELECT tblstudent.first, tblstudent.last,
            LEFT(viewactualstudenthours.ACTUAL_remain,5),
            (SELECT first from tbladdress where tbladdress.id = tblstudent.contact2),
            (SELECT last  from tbladdress where tbladdress.id = tblstudent.contact2),
            tbladdress.address1,tbladdress.city,tbladdress.state,tbladdress.zip1,
            tbladdress.phone, tbladdress.cell, tbladdress.email
            FROM tblstudent, tbladdress, tblstudentstatus, viewactualstudenthours
            WHERE viewactualstudenthours.student_id = tblstudent.id
              AND tblstudent.status = tblstudentstatus.id
              AND tbladdress.id = tblstudent.contact1
              AND tblstudentstatus.id = 3");

(Note: the editor made the SQL semi-legible - but probably broke every rule in the PHP code book.)

Upvotes: 0

Views: 58

Answers (1)

Jonathan Leffler
Jonathan Leffler

Reputation: 753785

Learn to use the explicit join notation introduced in SQL-92 instead of the older comma-separated list of table names in the FROM clause.

You need to use a LEFT OUTER JOIN of the table tblstudent with the view viewactualstudenthours. Ignoring the quotes etc needed to make the code work in PHP, you need:

SELECT S.first, S.last,
       H.ACTUAL_remain,
       A2.first, A2.last,
       A1.address1, A1.city, A1.state, A1.zip1,
       A1.phone,    A1.cell, A1.email
  FROM tblstudent                  AS S
  JOIN tbladdress                  AS A1 ON S.Contact1 = A1.ID
  JOIN tbladdress                  AS A2 ON S.Contact2 = A2.ID
  JOIN tblstudentstatus            AS T  ON S.Status   = T.ID
  LEFT JOIN viewactualstudenthours AS H  ON S.ID       = H.Student_ID
 WHERE T.id = 3

Also learn to use table aliases (the AS clauses) - it simplifies and clarifies the SQL. And if the schema is up to you, don't prefix the table names with 'tbl' and the view names with 'view' - it is just so much clutter.

Note that I got rid of the sub-selects in the select-list by joining to the Address table twice, with two separate aliases. I removed the function LEFT(); you can reintroduce it if you need to.

Upvotes: 1

Related Questions