This is my setup:
- Table “files”: id (PK), filename, user_id, date, filesize
- Table “scores”: id(PK), file_id, user_id, score
Table “files” contains a list of files with details; table “scores” keeps track of 1-5 points scored per file. I need to get entries from the “files” table and in each row I need all the info for the file, as well as the average score. I can do another query for teh current file_id while I’m looping through the rows, but obviousely that’s not very optimized. I tried something like below, but no success.
SELECT files.*, (SUM(scores.score)/(COUNT(scores.score))) AS total FROM files INNER JOIN scores ON files.id=scores.file_id;
Please point me in the right direction – thanks!
You may want to try the following:
Note that you can use the
AVG()aggregate function. There is no need to divide theSUM()by theCOUNT().Test case:
Result:
Note that @Ignacio’s solution produces the same result, and is therefore another option.