Mysql – How to i use the same query for all codes

MySQL

I use this query in PHPmyAdmin, i have date that contain(date,hour),supp, price and code_product (it repeated many times) :

I want to return the last price for the last date of product code

   SELECT invent.code_product,products.supp,products.price,products.date  
   FROM products

   LEFT JOIN list_product ON products.supp = list_product.supp
   LEFT JOIN invent on list_product.id_product = invent.fk_product

   WHERE invent.code_product= "XXX185"
   ORDER BY products.date  DESC
   LIMIT 1 ;

This is the result for one code :

enter image description here

The query works for one code and limit it the result in 1.

How can i use the same query to return the last date and price for all codes?

Best Answer

It sounds like you want something like this:

SELECT invent.code_product,products.supp,products.price, 
       MAX(products.date) AS Date
FROM products 
LEFT JOIN list_product ON products.supp = list_product.supp
LEFT JOIN invent on list_product.id_product = invent.fk_product
Group by  invent.code_product,products.supp,products.price
Order by  products.date

To fully understand your comments on Date and Hour, it would be good to see your data definition and some representative data.