I don't really like the database guru, so I need help with the query I'm working on. In my photography community project, I want to richly visualize tags, not only displaying the tag name and counter (the number of images inside them), but also want to show the thumb of the most popular image inside the tag (most of the karma).
The table setting is as follows:
- The image table contains the basic metadata of the image, the karma field is important.
- The image file table contains several entries per image, one for each format
- The tag table contains tag definitions.
- Tag_map table displays tags on images
In my usual trial and authorized error request, I went this far:
SELECT * FROM
(SELECT tag.name, tag.id, COUNT(tag_map.tag_id) as cnt
FROM tag INNER JOIN tag_map ON (tag.id = tag_map.tag_id)
INNER JOIN image ON tag_map.image_id = image.id
INNER JOIN imagefile on image.id = imagefile.image_id
WHERE imagefile.type = 'smallthumb'
GROUP BY tag.name
ORDER BY cnt DESC)
as T1 WHERE cnt > 0 ORDER BY cnt DESC
[column sentence of internal query compressed for simplicity)
This query gives me what I need. An external query ensures that only tags for which there is at least 1 image are returned. An internal query returns tag data, such as its name, counter (number of images), and thumb. In addition, I can sort the internal query as I see fit (for most images, in alphabetical order, recently, etc.)
So far so good. However, the problem is that this request does not match the most popular image (the karma itself) of the tag; it seems to always accept the latest in the tag.
How can I make sure the most popular image matches the tag?