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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T22:58:15+00:00 2026-06-15T22:58:15+00:00

long time lurker here, been using the advice from this site for years, and

  • 0

long time lurker here, been using the advice from this site for years, and now I have a question that I can’t find an answer to – I’m sure the solution is simple though!

I’m setting up a tagging system for music tracks. I have three tables

Tracks

---------------
| id | name   |
---------------
| 1  | Track1 |
| 2  | Track2 |
---------------

Taglinks

--------------------------
| id | tag_id | track_id |
--------------------------
| 1  | 1      | 1        | 
| 2  | 2      | 1        | 
| 3  | 2      | 2        |
--------------------------

Tags

-------------------------
| id | tag      | type  |
-------------------------
| 1  | acoustic | genre | 
| 2  | anger    | mood  |
-------------------------

I want to be able to get the track name of tracks which have, for example, tag:acoustic, type:genre AND tag:anger, type:mood. So in this case I want track 1 as this has both tags.

The current query I have is

SELECT tracks.name 
FROM tracks
JOIN taglinks ON tracks.id=taglinks.track_id
JOIN tags ON (tags.type='genre' AND tags.tag='acoustic') AND (tags.type='mood' AND tags.tag='anger')
WHERE taglinks.tag_id=tags.id;

This returns nothing, if I remove the AND section in the second JOIN then I get all tracks tagged acoustic as expected and if I change it to an OR I get both tracks, again as expected.

How do I do an AND though and return only those tracks that have both those tag and type?

Thanks in advance 😀

EDIT:

Just to clarify what I am after. I want to be able to retrieve tracks that have both Acoustic and Anger as tags, but only when those tags also match the given type.

This is because later I might have a tag called jazz with type genre, and a tag called jazz with type style and I need to be able to make sure I am selecting by the correct type of tag as well as the tag itself.

Here’s the link to the sqlfiddle I’m working with – http://sqlfiddle.com/#!2/aad82/3.

Gilles’s answer seems to be a good solution so far!

  • 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-06-15T22:58:16+00:00Added an answer on June 15, 2026 at 10:58 pm

    The problem is that the tracks should have the 2 tags equals to the asked values, all request posted yet will not work in that situation. The must should be to use the INTERSECT sql command but this one is not available in mysql.

    Something like that should work :

    SELECT tracks.id, tracks.name
    FROM  `taglinks` 
    LEFT JOIN tags ON tags.id = taglinks.tag_id
    LEFT JOIN tracks ON tracks.id = taglinks.track_id
    WHERE (
    tag =  'accoustic'
    AND `type` =  'genre'
    )
    OR (
    tag =  'anger'
    AND `type`=  'mood'
    )
    GROUP BY track_id
    HAVING COUNT( * ) =2
    
    • I select all rows which match at least one tag
    • I count the number of tage that match with a GROUP BY.
    • If I search two parameters (here accoustic = 'genre' and anger =
      'mood'
      and found 2 rows for this track then it matches the 2
      conditions and I keep it to the final result
    • Just remember to change the HAVING COUNT( * ) =2 with the number of
      tag we want to search
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've been a long time browser here, but never have had a question that
Long time lurker, first time poster here. My question is: Using C# 2.0, is
This is my first post, have been a lurker for a long time, so
Long time lurker, first time poster. I have a page that can dynamically (with
Long time lurker, first time poster. I have found some good answers on here
First time poster, long time lurker :) I have a form setup that posts
Newly registered user here after being a long time lurker! I have an Apache
Long time lurcher, first time poster here. I've been grappling with this problem for
Long term lurker, first time poster here. I have written a class to model
long time lurker, first question! I am struggling to optimize this query, which selects

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.