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

Seleccionar valores distintos en una tabla unida

SELECT T1.id, T1.date
FROM (SELECT A.id id, B.foo foo, A.date date
      FROM A JOIN B
      ON A.b_id = B.id) T1
JOIN (
    SELECT B.foo foo, MAX(A.date) maxdate
    FROM A JOIN B
    ON A.b_id = B.id
    GROUP BY B.foo) T2
ON T1.foo = T2.foo and T1.date = T2.maxdate
ORDER BY T1.date DESC
LIMIT 10