I'm trying to follow the answer from this question.
However it's not working for me and I can't see why.
SELECT h.*
FROM `student-history` h
LEFT OUTER JOIN `student-history` h2 ON h.id = h2.id AND h.`school-year` < h2.`school-year`
WHERE
h2.id IS NULL
My SQL is pretty much exactly the same as Adrian's in his answer yet its not producing the same results. Basically the student-history
table has a tinyint(1)
column called school-year
Which is typically a number between 1-3. Each user could have multiple entries. So I want to find the highest year row.
I don't totally understand his answer so I'm not really able to proceed any further but if anyone can help me as to where I'm going wrong that would be great.
If I add to the WHERE
clause in the above statement to narrow it down by user, such as AND h.userID = 54
, I still get three rows with all three years the student was present. The LEFT OUTER JOIN
doesnt seem to be making a difference at all.
I've created an SQL Fiddle here.