简体   繁体   中英

MySQL Get latest date where dependant latest insert

I've got two queries I am trying to join together so I don't have to perform one after the other, as in some cases I want to change the output order.

The 1st query, gets an invoices status. It changes to keep a log, but 99% of the time, I only need to check against the latest entry. The layout is as follows:

providers_invoice_status_ID, providers_invoice_summary_ID, status_ID, userinfo_ID, providers_invoice_status_date

SELECT providers_invoice_summary_ID
  FROM db_providers_invoice_status
 WHERE status_ID = '$status'
 ORDER BY providers_invoice_status_date DESC

The invoice table is then queried based on the results of the above query. It is laid out as follows.

providers_invoice_summary_ID, providers_invoice_summary_file, providers_invoice_summary_total, providers_invoice_summary_due, providers_invoice_summary_generated

    SELECT si.providers_invoice_summary_ID
     , providers_invoice_summary_file
     , providers_invoice_summary_total
     , providers_invoice_summary_due
     , providers_invoice_summary_generated
  FROM db_providers_invoice_summary si 
 WHERE providers_invoice_summary_ID = '$invoice_ID';

May attempts to merge the queries, resulted in this:

SELECT si.providers_invoice_summary_ID
     , providers_invoice_summary_file
     , providers_invoice_summary_total
     , providers_invoice_summary_due
     , providers_invoice_summary_generated
     , s.status_ID 
  FROM db_providers_invoice_summary si, db_providers_invoice_status s
 WHERE status_ID = ( SELECT status_ID
                   FROM db_providers_invoice_status
                  WHERE providers_invoice_summary_ID = si.providers_invoice_summary_ID 
                    AND status_ID = 7  
                  ORDER 
                     BY providers_invoice_status_date DESC 
                  LIMIT 1)

However, it pulls all the results from the status logging table where the status_ID = 7, so not quite getting the latest insert (the status could have since changed to 8, so dont want that invoice_ID). I've tried to use ORDER BY but its only ordering all the results, not quite the sub-set i'm after.

Any help on this would be hugely appreciated. Hopefully this is fairly clear to those reading it I know its pretty convoluted.

EDIT DATA:

Invoice Table:

providers_invoice_summary_ID providers_invoice_summary_file providers_invoice_summary_total providers_invoice_summary_due providers_invoice_summary_generated
----------------------------------------------------------------------------------------------------------------------------------------------------------
           4                |        ../blah/blah.jpg      |            245.63       |           2014-04-20               |           2014-03-14
           5                |        ../blah/blah.jpg      |            456.89       |           2014-04-20               |           2014-03-12
           6                |        ../blah/blah.jpg      |            125.36       |           2014-04-24               |           2014-03-12    

Status Table:

providers_invoice_status_ID providers_invoice_summary_ID status_ID userinfo_ID providers_invoice_Status_date
------------------------------------------------------------------------------------------------------------
               4           |              4             |   7     |      7    |   2014-03-14 10:19:41
               5           |              5             |   7     |      7    |   2014-03-12 10:22:41
               6           |              6             |   7     |      7    |   2014-03-24 10:15:38
               7           |              5             |   8     |      7    |   2014-03-26 11:15:14

I would expect to get 2 invoices out of this data, as two invoices currently have their latest status set to 7. Hope this clarifies things further.

Here's a combined version of the two queries. I created a subquery to select the invoices whose status matches the input status id (eg 7). The status ID, in turn, is fetched from a subquery using the latest status date.

UPDATED QUERY :

SELECT 
 si.providers_invoice_summary_ID
 , si.providers_invoice_summary_file
 , si.providers_invoice_summary_total
 , si.providers_invoice_summary_due
 , si.providers_invoice_summary_generated
 , invoice_status.status_ID 
FROM 
  db_providers_invoice_summary si, 
  (SELECT t1.providers_invoice_summary_ID as providers_invoice_summary_ID
      , t1.status_id as status_id
   FROM db_providers_invoice_status t1
  LEFT JOIN db_providers_invoice_status t2 ON t1.providers_invoice_summary_ID = t2.providers_invoice_summary_ID AND t1.providers_invoice_Status_date < t2.providers_invoice_Status_date
WHERE t2.providers_invoice_summary_ID IS NULL
  AND t1.status_id = 7
   ) invoice_status
WHERE SI.PROVIDERS_INVOICE_SUMMARY_ID = invoice_status.PROVIDERS_INVOICE_SUMMARY_ID

Please check out the sql fiddle for a demo as well.

References :

MySQL: The Rows Holding the Group-wise Maximum of a Certain Column

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