Obtenga la última fecha de los datos MySQL agrupados


Tengo los siguientes datos en mi base de datos:

|NO | model | date     | 
+---+-------+----------+
|1  | bee   |2011-12-01|
|2  | bee   |2011-12-05|
|3  | bee   |2011-12-12|
|4  | tar   |2011-12-13|

Quiero obtener la última fecha de cada grupo de modelos:

| model | date     | 
+-------+----------+
| bee   |2011-12-12|
| tar   |2011-12-13|

Lo intenté:

SELECT model, date 
FROM doc
WHERE date ........????? //what is the next?
GROUP BY model
Author: Mark Rotteveel, 2011-12-12

8 answers

¿ Está buscando la fecha máxima para cada modelo?

SELECT model, max(date) FROM doc
GROUP BY model

Si está buscando todos los modelos que coincidan con la fecha máxima de toda la tabla...

SELECT model, date FROM doc
WHERE date IN (SELECT max(date) FROM doc)
 78
Author: phatfingers,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2011-12-12 05:48:43

Puedes intentar usar max () en subconsulta, algo como esto:

SELECT model, date  
FROM doc 
WHERE date in (SELECT MAX(date) from doc GROUP BY model);
 10
Author: gprathour,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2011-12-12 05:40:44

Subconsulta dando fechas. No estamos enlazando con el modelo. Así que a continuación consulta resuelve el problema.

Si hay fechas/modelo duplicados se puede evitar mediante la siguiente consulta.

select t.model, t.date
from doc t
inner join (select model, max(date) as MaxDate from doc  group by model)
tm on t.model = tm.model and t.date = tm.MaxDate
 5
Author: Madhu Kiran Seelam,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2017-06-05 13:39:55

Prueba esto:

SELECT model, date
FROM doc
WHERE date = (SELECT MAX(date)
FROM doc GROUP BY model LIMIT 0, 1)
GROUP BY model
 0
Author: Christopher Pelayo,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2011-12-12 05:41:04

Usar max(date) no resolvió mi problema, ya que no hay garantía de que otras columnas sean de la misma fila que max(date). En lugar de eso, este resolvió mi problema y ordenó el grupo en un orden correcto y los valores de otras columnas son de la misma fila que la fecha máxima es:

SELECT model, date 
FROM (SELECT * FROM doc ORDER BY date DESC) as sortedTable
GROUP BY model
 0
Author: Iman Sedighi,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2018-02-28 20:23:59

Esto debería funcionar:

SELECT model, date FROM doc GROUP BY model ORDER BY date DESC

Simplemente ordena las fechas de la última a la primera y agrupando solo agarra la primera.

 -2
Author: Aldarien,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2014-10-09 14:06:02

¿Y por qué no usar esto ?

SELECT model, date FROM doc ORDER BY date DESC LIMIT 1
 -2
Author: Vasekdvor,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2017-07-27 16:21:11
SELECT model, date 
FROM doc
WHERE date = '2011-12-12'

Ni siquiera necesitas grupo por

Si usted está buscando la fecha máxima, (ur ejemplo no sugiere esto por cierto)

SELECT model, MAX(date) 
FROM doc
WHERE date = '2011-12-12'
group by model
 -4
Author: Zohaib,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2011-12-12 05:35:06