I have a question with a query, I need to make a query that shows me the most recent date of the registration of a price of a product that has a record of several prices with different dates, I just want to obtain the most recent date for each product, and for this I have the following tables
and my query is as follows
SELECT hp.id_historico,
p.id_producto,
p.codigo_producto,
p.nomenclatura,
p.subnomenclatura,
p.clasificacion,
p.unidad_medida,
p.subcategoria,
p.nombre_producto,
p.gramaje,
p.entrada_salida,
p.status_producto,
p.stock,
p.id_categoria,
p.last_code,
CONCAT( u.firstname, ' ', u.lastname, ' ', u.user_perfil ) AS USER,
m.marca,
hp.precio_unitario,
hp.iva,
hp.precio_venta,
hp.fecha
FROM h_precio_prod hp,
products p,
users u,
marca m
WHERE hp.id_producto = p.id_producto
AND hp.user_id = u.user_id
AND hp.id_marca = m.id_marca
GROUP BY hp.id_producto
ORDER BY hp.fecha DESC
The detail with my query is that it shows me the oldest date instead of the most recent and when using a sub query it returns an error in the format of the date
Subquery
SELECT hp.id_historico,
p.id_producto,
p.codigo_producto,
p.nomenclatura,
p.subnomenclatura,
p.clasificacion,
p.unidad_medida,
p.subcategoria,
p.nombre_producto,
p.gramaje,
p.entrada_salida,
p.status_producto,
p.stock,
p.id_categoria,
p.last_code,
CONCAT( u.firstname, ' ', u.lastname, ' ', u.user_perfil ) AS USER,
m.marca,
hp.precio_unitario,
hp.iva,
hp.precio_venta,
hp.fecha
FROM h_precio_prod hp,
products p,
users u,
marca m
WHERE hp.id_producto = (SELECT MAX(hp.fecha) FROM h_precio_prod hp)
AND hp.user_id = u.user_id
AND hp.id_marca = m.id_marca
GROUP BY hp.id_producto ORDER BY hp.fecha DESC