Tables
file_logs
- file_id
- user_id
files
- id
- name
- etc ...
published_ratings
- author_id
- file_id
- comment
- etc ...
Scenario
I am creating a download log and need to display which files a user has rated as well as the unrated ones, this has to be done with 1 query.
I already took a crack at this,
SELECT
files.*,
IF(file_logs.user_id = published_file_ratings.author_id, TRUE, FALSE) AS rated
FROM file_logs
JOIN files
ON file_logs.file_id = files.id
LEFT JOIN published_file_ratings
ON file_logs.file_id = published_file_ratings.file_id
WHERE file_logs.user_id = 8
GROUP BY id
The problem with this query is that if an incorrect user came up first the results would be wrong.
The result should be an array of all the files the user has downloaded with a "rated" column depicting if they've rated the file or not.
Any help is really appreciated.