Using MYSQL, I would like to reorganize the following statement SELECTto return an entire record containing the latestinvoice_date :
> SELECT id, invoice, invoice_date
FROM invoice_items
WHERE lot = 1047
id invoice_id invoice_date
-----------------------------------
3235 1047 2009-12-15 11:40:00
3295 1047 2009-12-15 16:00:00
3311 1047 2009-12-15 09:30:00
3340 1047 2009-12-15 13:50:00
Using the MAX () aggregation function and the GROUP BY clause gives me part of the path:
> SELECT id, invoice_id, max(invoice_date)
FROM invoice_items
WHERE invoice_id = 1047
GROUP BY invoice_id
id invoice_id invoice_date
-----------------------------------
3235 1047 2009-12-15 16:00:00
Please note that the query looks correct if id, but the returned id(3235) is not idfor the record containing MAX(invoice_date)(3295), this is the idfirst record in the original query.
How do I reorganize this request to give me a complete record containing MAX(invoice_date)?
The solution is to use the GROUP BY clause because I need to get the latest one invoice_datefor each invoice.