简体   繁体   中英

mysql inner join 3 tables get last record from max date

I have 3 tables which i would like joined but with some addition to summing certain columns I need to display every user_id and user name in the database but also joining their corresponding accounts tables with some addition and date based query. Here is an example of what im trying to say.

Table Users

user_id || user_name || user_dob    
    301 || john doe   ||  1955-01-01    
    312 || Bill Gates || 1976-01-01

Table accounts

 id  || child_id ||          inv_date     || inv_total || inv_funding    
 38  ||   301    ||  2018-05-03 12:56:38  ||    486.5  ||   45.55     
 39  ||   301    ||  2018-08-03 14:56:38  ||    222.5  ||   118.5     
 40  ||   312    ||  2018-04-03 11:56:38  ||    26.23  ||   318.5     
 41  ||   312    ||  2018-05-03 12:56:38  ||    223.22 ||   238.5     
 42  ||   312    ||  2018-06-03 13:56:38  ||    486.5  ||   258.5 

Table accounts_balance

id || child_id || balance    
1  ||    301   || 302.00    
2  ||    312   || 43.33

The resulting table needs to look like this for every user in Table Users ordered by user_name

User Name || Last Invoiced Date || Last Funding Amount || Last Invoice Amount || Total Funding To Date || Total Invoiced To Date || Balance
john doe  || 2018-08-03 14:56:38||      118.5          ||        222.5        ||        164.05         ||        712.00          ||    302.00

here is some of my code excuse the c# formating clearly not working well.

            invoiceTable.Query = "select  " +
                "(" +
                "max(users.user_id), " +
                "users.user_name, " +
                "max(accounts.inv_date), " +
                "max(accounts.inv_funding) as Last_funding, " +
                "max(accounts.inv_total) as Last_Invoice, " +
                "sum(accounts.inv_funding) as Total_Funding, " +
                "sum(accounts.inv_total) as Total_Invoiced from users " +
                ")" +
                "left join accounts on accounts.child_id = users.user_id group by users.user_name";

you can try by using sub-query

          select t1.*,t2.inv_funding as Last_funding,t2.inv_total as 
          Last_Invoice,ab.balance  from
               (
                select users.user_id, 
                users.user_name, 
                max(accounts.inv_date) as inv_date,
                sum(inv_funding) as Total_Funding,
                sum(inv_total) as Total_Invoiced 
                from users                    
                left join accounts on accounts.child_id = users.user_id
                group by users.user_name,users.user_id 
             ) as t1
         left join accounts t2 on t1.inv_date=t2.inv_date
         left join accounts_balance ab on t1.user_id =ab.user_id 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM