sql >> Base de Datos >  >> RDS >> Mysql

mysql:¿Cómo INNER JOIN en una tabla pero limitar la unión a 1 resultado con el voto o recuento más alto?

SELECT a.*, b.*
  FROM Items a
       LEFT JOIN Votes b on a.item_id = b.item_id
                         and b.total_yes = (select max(total_yes) 
                                              from Votes v 
                         where v.item_id = a.item_id)
ORDER BY a.post_date DESC, b.total_yes DESC

N.B.:si tiene para un ítem 2 respuestas con el mismo total_yes =max, tendrá 2 filas para ese ítem.