Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 810375
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T00:52:29+00:00 2026-05-15T00:52:29+00:00

I’m not much of a database guru so I need some help on a

  • 0

I’m not much of a database guru so I need some help on a query I’m working on. In my photo community project I want to richly visualize tags by not only showing the tag name and counter (# of images inside them), I also want to show a thumb of the most popular image inside the tag (most karma).

The table setup is as follow:

  • Image table holds basic image metadata, important is the karma field
  • Imagefile table holds multiple entries per image, one for each format
  • Tag table holds tag definitions
  • Tag_map table maps tags to images

In my usual trial and error query authoring I have come 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 clause of inner query snipped for the sake of simplicity]

This query gives me somewhat what I need. The outer query makes sure that only tags are returned for which there is at least 1 image. The inner query returns the tag details, such as its name, count (# of images) and the thumb. In addition, I can sort the inner query as I want (by most images, alphabetically, most recent, etc)

So far so good. The problem however is that this query does not match the most popular image (most karma) of the tag, it seems to always take the most recent one in the tag.

How can I make sure that the most popular image is matched with the tag?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T00:52:29+00:00Added an answer on May 15, 2026 at 12:52 am

    This should be pretty close:

    SELECT
      tag.id, 
      tag.name,
      tag_group.cnt,
      tag_group.max_karma,
      image.id, 
      imagefile.filename
      /* ... */
    FROM
      tag
      /* join against a list of max karma values (per tag) */
      INNER JOIN (
        SELECT   MAX(image.karma) AS max_karma, COUNT(image.*) cnt, tag_map.tag_id
        FROM     image
                 INNER JOIN tag_map ON tag_map.image_id = image.id
        GROUP BY tag_map.tag_id
      ) AS tag_group ON tag_group.tag_id = tag.id
      /* join against a list of image ids (per max karma value and tag) */
      INNER JOIN (
        SELECT   MAX(image.id) id, tag_map.tag_id, image.karma
        FROM     image
                 INNER JOIN tag_map ON tag_map.image_id = image.id
        GROUP BY tag_map.tag_id, image.karma /* collapse >1 imgs with same karma */
      ) AS pop_img ON pop_img.tag_id = tag.id AND pop_img.karma = tag_group.max_karma
      /* join against actual base data (per popular image id) */
      INNER JOIN 
        image ON image.id = pop_img.id
      INNER JOIN
        imagefile ON imagefile.image_id = pop_img.id AND imagefile.type = 'smallthumb'
    

    Basically, this is the ever-recurring “max-per-group” problem: How can I select the record that corresponds to the maximum/minimum value of a group?

    And the general answer always is along the lines of: Select your group (tag_id, MAX(image.karma)) and then join your base data against these characteristics. There may be DBMS-specific proprietary extensions that take a different approach, for example using ROW_NUMBER()/PARTITION BY. However, these are not very portable and may leave you scratching your head when working with a DBMS that does not support them.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need a function that will clean a strings' special characters. I do NOT
I have just tried to save a simple *.rtf file with some websites and
For some reason, after submitting a string like this Jack’s Spindle from a text
I want to count how many characters a certain string has in PHP, but
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I want use html5's new tag to play a wav file (currently only supported
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I need to clean up various Word 'smart' characters in user input, including but

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.