Note that there are some explanatory texts on larger screens.

plurals
  1. POmysql-php querying a list to build a table
    primarykey
    data
    text
    <p>I've had the same problem with sql for a while and as I sit down to write the same 'brute-force' hack I always use, I figure there MUST be a more efficient way to do what I want to do.</p> <p>I have tables similar to this:</p> <pre><code>grades(gradeID, taskID, grade, username, date) tasks(taskID, task...) assignment(assignmentID, title...) assignment_tasks(assignmentID, taskID) assignment_students(assignmentID, username) students(username, forename...) </code></pre> <p>the last 5 tables are pretty static, set up once and mostly left alone.</p> <p>for the grades table, a new record is created every time a new grade is entered for a task.</p> <p>I want to produce a summary table for an assignment, which might consist of say, 5 tasks and each student may have any number of grades for each task, with the most recent one being the one I want to display in the summary.</p> <p>What I'd normally do is query a list of students, and a list of assignment tasks, then build a giant nested for loop looping through each task for each student, querying the most recent grade for each, so assuming 30 students, 5 tasks, that's 152 queries, which has always struck me as waaay too many.</p> <p>I figure (hope) I have a horribly embarrassing gap in my sql knowledge and there's a much cleverer way to do it.</p> <p>Edit: Thanks for the answer - I'm still working on building the actual database so I can test it, but I suspect that the answer below doesn't cover the following issues:</p> <p>if a student hasn't attempted a task/assignment, there won't be any entries for them in the grades table but they still need to show in the summary table with a default grade for each task ("u"). I think this bit makes it harder.</p> <p>Edit again: I have the embryo database now and it works in that I get a list of most recent grades with gaps where there's no grade. Transposing that list is now the subject of <a href="https://stackoverflow.com/questions/1000716/php-list-into-a-table">another question</a>!</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload