Mysql
 sql >> Database >  >> RDS >> Mysql

Selezione di valori distinti in una tabella unita

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