So, I have this query in PHP which is working as intended:
SELECT files.ID,
files.Name,
files.Downloads,
if (dlratings.userID IS NULL,0,r.vote) AS has_voted
FROM files
LEFT JOIN dlratings
ON (files.ID = dlratings.fileID AND dlratings.userID = $userID)
WHERE files.Temporary != 1 AND files.Type = $type AND files.Cat_1 = $cat_1
ORDER BY files.rating DESC, files.DateOrder DESC
Now comes my problem: I want to also select the tags with which these selected files are associated. I am using the toxi method, meaning I have a table called “tagmap” with the rows file_id and tag_id and I have the table “tags” with the rows tag_id and tag_name.
To list the tags I need on a page with only one File, I use this query:
SELECT files.Name,
GROUP_CONCAT(tags.tag_name ORDER BY tags.tag_name SEPARATOR ', ') AS tags
FROM files
LEFT JOIN tagmap
ON tagmap.file_id = files.ID
LEFT JOIN tags
ON tagmap.tag_id = tags.tag_id
WHERE files.ID = $fileID
How do I combine the two, so I can show the tags in the list that is provided by the first query?
This is what I tried but it only gives me 1 result:
SELECT files.ID,
files.Name,
files.Downloads,
GROUP_CONCAT(tags.tag_name ORDER BY tags.tag_name SEPARATOR ', ') AS tags
if (dlratings.userID IS NULL,0,r.vote) AS has_voted
FROM files
LEFT JOIN tagmap
ON tagmap.file_id = files.ID
LEFT JOIN tags
ON tagmap.tag_id = tags.tag_id
LEFT JOIN dlratings
ON (files.ID = dlratings.fileID AND dlratings.userID = $userID)
WHERE files.Temporary != 1 AND files.Type = $type AND files.Cat_1 = $cat_1
ORDER BY files.rating DESC, files.DateOrder DESC
Note that I am calling these querys via php and I use mysqli. I experimented a bit with the mysqli_multi_query but I am totally lost on that one too…
Try adding a GROUP BY files.ID. That might do it?