Tengo dos tablas y trato de encontrar la "publicación" con la puntuación más alta por día.MySQL group by y max devuelve filas incorrectas
CREATE TABLE IF NOT EXISTS `posts_points` (
`post_id` int(10) unsigned NOT NULL,
`comments` smallint(5) unsigned NOT NULL,
`likes` smallint(5) unsigned NOT NULL,
`favorites` smallint(5) unsigned NOT NULL,
PRIMARY KEY (`post_id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
CREATE TABLE IF NOT EXISTS `posts` (
`profile_id` int(10) unsigned NOT NULL,
`post_id` int(10) unsigned NOT NULL,
`pubdate_utc` datetime NOT NULL,
PRIMARY KEY (`post_id`),
KEY `profile_id` (`profile_id`),
) ENGINE=MyISAM DEFAULT CHARSET=utf8 ROW_FORMAT=DYNAMIC;
He intentado la consulta a continuación. Devuelve el puntaje correcto pero las otras columnas son solo filas aleatorias. Qué estoy haciendo mal ?
SELECT p.post_id, p.profile_id
, MAX(t1.score)
, DATE_FORMAT(t1.pubdate_utc, '%d %b') post_date
, DATE(t1.pubdate_utc) mydate
FROM
(
SELECT p.profile_id, p.post_id, p.pubdate_utc
, (pp.comments + pp.likes + pp.favorites) AS score
FROM posts p
INNER JOIN posts_points pp ON p.post_id = pp.post_id
) t1
INNER JOIN posts p ON t1.post_id = p.post_id
AND t1.pubdate_utc = p.pubdate_utc
GROUP BY mydate
ORDER BY mydate DESC
LIMIT 18;
1 para la inclusión de la definición de tabla – ManseUK
No estás haciendo nada malo.Las funciones agregadas no afectan el valor de otras columnas. Parecerán ser "aleatorias". –
Agrupar por fecha debería generar ese comportamiento extraño. – Alfabravo