Get latest record from second table left joined to first table

Posted by codef0rmer on Stack Overflow See other posts from Stack Overflow or by codef0rmer
Published on 2010-06-15T08:10:36Z Indexed on 2010/06/15 8:12 UTC
Read the original article Hit count: 166

Filed under:
|

I have a candidate table say candidates having only id field and i left joined profiles table to it. Table profiles has 2 fields namely, candidate_id & name.

e.g. Table candidates: id 1 2

 and Table `profiles`: 
 candidate_id      name
      1           Foobar
      1           Foobar2
      2           Foobar3

i want the latest name of a candidate in a single query which is given below:

SELECT C.id, P.name FROM candidates C LEFT JOIN profiles P ON P.candidate_id = C.id GROUP BY C.id ORDER BY P.name;

But this query returns: 1 Foobar 2 Foobar3 Instead of: 1 Foobar2 2 Foobar3

© Stack Overflow or respective owner

Related posts about mysql

Related posts about mysql-query