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

MySQL piace un join ma serve solo la riga più recente?

Puoi farlo usando il seguente SQL:

SELECT name,
       (
        SELECT datecreated
          FROM tbl_comments
          WHERE tbl_users.user_id = tbl_comments.user_id
          ORDER BY datecreated LIMIT 1
       )
FROM tbl_users
WHERE category = '1';

OPPURE utilizzando:

SELECT tbl_users.name, MAX(datecreated) AS latestcomment
  FROM tbl_users LEFT JOIN tbl_comments ON (tbl_users.user_id = tbl_comments.user_id)
  GROUP BY tbl_users.name;