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

  • SEARCH
  • Home
  • 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 5840205
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T11:42:27+00:00 2026-05-22T11:42:27+00:00

I am trying to return a list of ungrouped values having a max defined

  • 0

I am trying to return a list of ungrouped values having a max defined number of repeating values. I have a list of values retrieved as such:

select TagDirID from tags where id = '550'

The results would be:

9508
10382
10672
65454
65454
65454
65454

As you can see there are 4 repeats of 65454. I would like to return a list that has a user defined max number of repeats for a TagDirID. For instance, selecting only 3 repeats or less:

9508
10382
10672
65454
65454
65454

All the methods I’ve found return a grouped list, i would like to maintain the individual items. Is this something that can be done in a query? There is a primary key available, TagID.

edit: what this does is select all the tags for an item, id = ‘550’. because it’s user content sometimes people tag the same things multiple times and i’m trying to cut down the duplicates i show.

edit 2: so, while the accepted answer worked for me i found it was a little too slow for what i needed so i came up with a php solution:

function get_tags($ID = '', $tags_to_keep = 3)
{   
    // Select all tags.
    $query = "select TagDirID, Tag from tags where id = '$ID'";

    $tags_result = mysql_query($query); 

    $num_results = mysql_num_rows($tags_result);

    for ($i=0; $i<$num_results; $i++)
    {
        //Get tag topics
        $tags_row = mysql_fetch_array($tags_result);

        //build array of items already found with counts
        $tags_count = array_count_values($tags_filter);

        //if number of tags already found($tags_count/$tags_filter) is less than or equal to tags_to_keep then add to filtered array and return array.
        if($tags_count[$tags_row['TagDirID']] <= $tags_to_keep)
        {
            $tags_filter[$i] = $tags_row['TagDirID'];
            $tags[$i] = $tags_row['Tag'];
        }           
    }

    return $tags;
}
  • 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-22T11:42:28+00:00Added an answer on May 22, 2026 at 11:42 am
    Select TagID, TagDirID
    From    (
            Select T1.TagID, T1.TagDirID
                 , (Select Count(*)
                    From tags As T2
                    Where T2.TagDirID = T1.TagDirID
                        And T2.TagID < T1.TagID) As Rnk
            Where T1.id = '550'
            From tags As T1
            ) As T
    Where T.Rnk < 3
    

    Another way of writing the same query:

    Select TagID, TagDirID
    From    (
            Select T1.TagID, T1.TagDirID, Count( T2.TagID ) As Rnk
            From tags As T1
                Left Join tags As T2
                    On T2.TagDirID = T1.TagDirID
                        And T2.TagID < T1.TagID
            Where T1.id = '550'
            Group By T1.TagID, T1.TagDirID
            ) As T
    Where T.Rnk < 3
    

    The approach here is to mimic a ranking function which would sequence the rows for each grouping of TagDirID. Thus, the inner query in either of the two above solutions should give you something like:

    TagID | TagDirID | Rnk
    1     | 9508     | 0
    2     | 10382    | 0
    3     | 10672    | 0
    4     | 65454    | 0
    5     | 65454    | 1
    6     | 65454    | 2
    7     | 65454    | 3
    

    With the rows numbered within each grouping, we can now filter down our results so that we only get a maximum number of rows in any given group. The ISO/ANSI solution would be to use the ROW_NUMBER ranking function which isn’t yet supported by MySQL.

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

Sidebar

Related Questions

i am trying to return a list of users whose accounts have expired from
I have a WCF service where I am trying to return a List (where
I am trying to return a List<Image> but am having trouble with the following
I'm trying to return a generic list after loading values from a file. However,
I'm trying to return a list of select options to a JqGrid Add feature.
I'm trying to return a list of course id's from my database with PDO
I am trying to return a list of object from my mapper and save
Im trying to return a SimpleQuery list that queries a single table and uses
I am trying to use LINQ to return a list of tasks that are
I created an integer list and am trying to return the index of a

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.