Skip to content Skip to sidebar Skip to footer

Returning Only Latest Result From Left Join

I am querying data from two tables (students2014 and notes2014) in order to return a list of students along with notes on each student. To do this I am using the following select s

Solution 1:

You need to join the studends table with a sub query. Something like this should work:

SELECT * 
FROM `students2014`
LEFT JOIN (
    SELECT `note`, `NoteStudent`
    FROM `notes2014`
    HAVING `NoteID` = MAX(`NoteID`)
    GROUP BY `NoteStudent`
) `notes`
ON `students2014`.`Student` = `notes`.`NoteStudent`
WHERE `students2014`.`Consultant`='$Consultant' 
ORDER BY `students2014`.`LastName`

Solution 2:

Try (I don't test it, but must work):

SELECT *, MAX(notes2014.notesID) as maxnoteid 
FROM students2014 
LEFT JOIN notes2014 ON students2014.Student = notes2014.NoteStudent
WHERE students2014.Consultant='$Consultant' AND notes2014.notesID = maxnoteid GROUPBY students2014.ID
ORDERBY students2014.LastName

Solution 3:

select
  *
from
  `students2014`,
  `notes2014`
where
  `students2014`.`Student` = `notes2014`.`atudent`and`notes2014`.`id` in (
    select'NoteStudent`,
     max('NoteID`) as`MaxID`
  from
    `notes2014`
  group by
    `NoteStudent`
  )`

Post a Comment for "Returning Only Latest Result From Left Join"