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 526783
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T08:48:48+00:00 2026-05-13T08:48:48+00:00

I have a MySQL database with various tables whose records can be tagged, so

  • 0

I have a MySQL database with various tables whose records can be tagged, so I have a tags table, and a tag_associations table that relates tags to other “taggable” tables via multiple foreign keys, and also to an “owning” user — my fields are something like the following:

  • tag_association_id (primary key)
  • user_id (tag association creator)
  • tag_id (related tag)
  • artist_id (tagged artist, can be null)
  • album_id (tagged album, can be null)
  • track_id (tagged track, can be null)

I basically want to count all items that have been tagged a particular tag — so something along the results of this query:

SELECT
    COUNT(ta.tag_association_id) AS how_many
FROM
    tag_associations AS ta
    LEFT JOIN users AS u ON ta.user_id = u.user_id
WHERE
    ta.tag_id = '480'
    AND u.user_status = 'active'

But the problem with this query lies in cases where the same tag has been applied to the same item by multiple users, so if 2 different users tag the artist ‘Bobby Jones Band’ as ‘rad’, it counts both of the tag associations where I just want to count it once. I tried adding this to the above:

GROUP BY ta.artist_id, ta.album_id, ta.track_id

…which got me close, but didn’t yield the exact results I needed — it gave me multiple row results of different counts. Is there any magic I can use in a case like this and keep it in a single query? While remaining as efficient as possible, of course 🙂 Thanks!

  • 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-13T08:48:48+00:00Added an answer on May 13, 2026 at 8:48 am

    If I got your question right, then your GROUP BY should almost do the job.

    This solution should get the unique rows from tag_associations and count them.

    SELECT COUNT(*)
    FROM
    (
        SELECT 1
        FROM tag_associations AS ta 
        LEFT JOIN users AS u ON ta.user_id = u.user_id
        WHERE ta.tag_id = '480' 
        AND u.user_status = 'active' 
        GROUP BY ta.artist_id, ta.album_id, ta.track_id
    ) x
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have stored various records in the MySql database orkut. Now I want to
I have mysql database and I want a software which can draw the database
I have mysql database structure like below: CREATE TABLE test ( id int(11) NOT
I have a MySQL database behind a firewall which can only be accessed via
I have a MySQL database table with a couple thousand rows. The table is
I have a MySQL database of keywords that are presently mixed-case. However, I want
I have a mysql database which has grown to over 200 tables in it.
I have a MySQL database that I want to archive . What is the
what are the various other programs using which i can connect to odbc database
I have 3 tables in a m2m relationship in a mysql database as follows:

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.