I have 2 tables, tbl_students & tbl_inv. The student table holds all information for students. I am creating an invoicing system to create and keep records for invoices (monthly) for the students. The tbl_inv can have multiple invoices for a single student but with a different date. I am trying to create an array which will list all the students with the latest invoice, total and status ordered by the newest invoice date at the top. I am fairly new to programming with mysql and php, so my apologies if my question sounds silly. This is what I have for the query…..
$query = "SELECT * FROM tbl_student
LEFT JOIN tbl_inv ON tbl_student.sid = tbl_inv.inv_sid
GROUP BY tbl_student.sid
ORDER BY tbl_inv.inv_date DESC";
This creates an array which has one line per student but doesn’t display the latest invoice date and details.
If anyone can help I would be much appreciated 🙂
(Addition pull from direct comments to existing answer)
This is the final query that works..
SELECT
S.*,
TI2.*
FROM
tbl_student S
LEFT JOIN ( SELECT
TI.inv_sid,
MAX(TI.inv_id) LatestInvoice
FROM
tbl_inv TI
GROUP BY
TI.inv_sid ) PreQuery
ON S.sid = PreQuery.inv_sid
LEFT JOIN tbl_inv TI2
ON PreQuery.inv_sid = TI2.inv_sid
AND PreQuery.LatestInvoice = TI2.inv_id
ORDER BY
TI2.inv_id ASC
I have students link with each other my using a field called f_link on the student table. If a student has a family member linked to them the f_link field shows the master id no. If there is no family link the field just shows a 0. With my invoicing system I am creating an invoice for a student and any family member linked to them. The current query will display no data for invoices for the family member, but they have been invoiced. I want to narrow this query down to only display students who have 0 in the f_link field.
Keep looking and learning querying… formatting and readability is a big plus for future maintenance. For this type of problem, ask what is the FIRST thing you need… in your case, on a per-student basis, what is the last invoice they had (regardless of the data).
The above will give you one piece of the puzzle, and that is how many complex queries get built. From this, you then want the student’s name, and invoice details about this most recent invoice. To do, the above query will be used in the next phase… in this case, it will get an “alias” for its result set to get the rest of the details.
So, to clarify above query
For EVERY Student (table listed first),
do a LEFT JOIN (all students regardless of ever yet having an invoice) to the “PreQuery” result set that has all students with their most recent invoice date).
If such an invoice record WAS found, do another LEFT JOIN to the invoice table AGAIN, but this time on the PreQuery “student id” and the “LatestInvoiceDate” determined for that user to the original invoice table.
Once those are all joined together, get all the fields from both the student table and the SECOND instance (TI2) invoice table for the invoice details.
Now, if you want ONLY students who HAD A MINIMUM of at least 1 invoice, just leave as “JOIN” instead of “LEFT JOIN”.