Return the data from the rows with the most recent date of each distinct candidate_id

后端 未结 2 1076
刺人心
刺人心 2021-01-05 06:40

I am attempting to return the data from the rows with the most recent date of each distinct candidate_id. It is correctly returning the most recent date (the created_unix co

2条回答
  •  情深已故
    2021-01-05 07:14

    SELECT m1.*
    FROM messages as m1
      LEFT OUTER JOIN messages AS m2
        ON    (m1.candidate_id = m2.candidate_id
          AND (m1.created_unix < m2.created_unix)
    WHERE m2.created_unix is NULL
    AND employer_id='$employerid' AND last='company'
    

    This joins messages to itself on candidate_id and makes rows with picks all dates in m2 that are greater than each date in m1, substituting NULL if none are greater. So you get NULL precisely when there is no greater date within that candidate_id.

提交回复
热议问题