S.Mart S.Mart - 4 months ago 17
MySQL Question

Select max and min date from registers

I have a table with these columns:

product
,
date
in MySQL database. The table saves access to the product with the date.

Product | Date

p1 | '2016-07-03 10:00:09.368000'

p2 | '2016-07-03 10:00:24.872000'

p1 | '2016-07-03 10:00:59.070000'

p2 | '2016-07-03 10:01:00.948000'


I'm trying to obtain (for each product) the max and the min date the product has been accessed but in just one query, I've been trying many things, and I havenĀ“t found a good solution.
Any ideas?

Answer

Did you mean this?

SELECT 
    Product,
    MAX(`Date`) AS maxDate,
    MIN(`Date`) AS minDate
FROM yourtable
GROUP BY Product
Comments