Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8916797
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T05:20:04+00:00 2026-06-15T05:20:04+00:00

I have 2 tables, tbl_students & tbl_inv. The student table holds all information for

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T05:20:06+00:00Added an answer on June 15, 2026 at 5:20 am

    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).

    select
          TI.inv_sid,
          MAX( TI.inv_date ) LatestInvoiceDate
       from
          tbl_inv TI
       group by
          TI.inv_sid
    

    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.

    select
          S.*,
          TI2.*
       from
          tbl_Student S
             LEFT JOIN  ( select
                                TI.inv_sid,
                                MAX( TI.invoicenumber ) LatestInvoiceNumber,
                                count(*) totalInvoicesForStudent,
                                sum( TI.Total ) as TotalAllInvoices,
                                sum( TI.Total - TI.AmountPaid) as OutstandingBalances
                             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.LatestInvoiceNumber = TI2.invoiceNumber
       where
          s.F_Link = 0
       order by
          TI2.inv_date desc
    

    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”.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have three tables that I need get information from, 1 table has the
I have got two tables: tbl_sms tbl_bids The above two table holds something like
I have a table that contains intervals: CREATE TABLE tbl ( user_id: INTEGER, start:
I have a table named tbl_Subjects_Taken which lists all the subjects taken by the
I have to copy data for one student (clone student) that encompass three tables:
I have two tables in my database, and I would like to retrieve information
I have 3 tables: tbl_Image from which a list of all images will be
I have two temp tables like this: declare @Tbl_A table ( Id int )
I have a table where I need to display the results of students in
I have a table which stores the courses registered by students during a semester

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.