While I'm still working with these tables I have another set of information I want to grab, but I'm not sure if this is even possible or how to do it if it is. I've researched MySQL.com and can't find anything that helps with what I want to do. If you have an idea I'd greatly appreciate to hear it.
Basically I want to return a "rank #" for each student based on the projects they have turned in. Say the projects table has the following fields:
id, student_id, project_name, project_time, project_order
I want to grab the rank based on the order ASC and then the time ASC. So for example, if this was my projects table, the ranks would be as follows:
1 | 1 | cheese | 2222 | 5 = rank 4
2 | 1 | water | 4444 | 4 = rank 2
3 | 2 | cheese | 1111 | 5 = rank 3
4 | 2 | water | 3333 | 4 = rank 1
5 | 3 | pizza | 0111 | 6 = rank 5
Here, the ORDER BY of the query would be (project_order, project_time), but I will be using the query posted by bubblegum.anarchy earlier, so I don't care about the ranks of the projects, I care about the ranks of students. Using the above example User 2 would be ranked #1, User 2 would be ranked #2, and User 3 as #3.
Using this query, is it possible to get each students rank using another JOIN?
SELECT students.id
, students.student_name
, count(projects.id) AS project_count
, latest_project.project_name
, latest_project.project_time
, something.something AS student_rank
FROM students
LEFT JOIN projects ON students.id = projects.student_id
LEFT JOIN (
SELECT projects.*
FROM projects
INNER JOIN (
SELECT student_id, max(project_time) AS latest_time
FROM projects GROUP BY projects.student_id
) AS latest_project ON projects.student_id = latest_project.student_id AND projects.project_time = latest_project.latest_time
GROUP BY projects.student_id
) AS latest_project ON projects.student_id = latest_project.student_id
LEFT JOIN something
GROUP BY students.id
I haven't even made an attempt at figuring this out as I have no idea where to start. If you have an idea what I'm talking about (I have the feeling my explanation was rather vague), please toss some advice my way.
P.S. I am shying away from adding a "rank" field to the students table because that will eventually require updating thousands of records for every insert, update, and delete done to the projects table.