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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T03:54:33+00:00 2026-05-15T03:54:33+00:00

I have two tables in a MySQL database, Locations and Tags, and a third

  • 0

I have two tables in a MySQL database, Locations and Tags, and a third table LocationsTagsAssoc which associates the two tables and treats them as a many-to-many relationship.

Table structure is as follows:

Locations
---------
ID int (Primary Key)
Name varchar(128)

LocationsTagsAssoc
------------------
ID int (Primary Key)
LocationID int (Foreign Key)
TagID int (Foreign Key)

Tags
----
ID int (Primary Key)
Name varchar(128)

So each location can be tagged with multiple tagwords, and each tagword can be tagged to multiple locations.

What I want to do is select only Locations which are tagged with all of the tag names supplied. For example:

I want all locations which are tagged with both “trees” and “swings”. Location “Park” should be selected, but location “Forest” should not.

Any insight would be appreciated. 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-15T03:54:33+00:00Added an answer on May 15, 2026 at 3:54 am

    There are two ways to do this. I prefer the first way, which is to self-join for each tag:

    SELECT l.*
    FROM Locations l
    JOIN LocationsTagsAssoc a1 ON a1.LocationID = l.ID
    JOIN Tags t1 ON a1.TagID = t1.ID AND t1.Name = ?
    JOIN LocationsTagsAssoc a2 ON a2.LocationID = l.ID
    JOIN Tags t2 ON a2.TagID = t2.ID AND t2.Name = ?
    JOIN LocationsTagsAssoc a3 ON a3.LocationID = l.ID
    JOIN Tags t3 ON a3.TagID = t3.ID AND t3.Name = ?;
    

    The other way also works, but using GROUP BY in MySQL tends to incur a temporary table and performance is slow:

    SELECT l.*
    FROM Locations l
    JOIN LocationsTagsAssoc a ON a.LocationID = l.ID
    JOIN Tags t ON a.TagID = t.ID
    WHERE t.Name IN (?, ?, ?)
    GROUP BY l.ID
    HAVING COUNT(*) = 3;
    

    Re comment from @Erikoenig:

    If you want to make sure there are no extra tags, you can do it this way:

    SELECT l.*
    FROM Locations l
    JOIN LocationsTagsAssoc a ON a.LocationID = l.ID
    JOIN Tags t ON a.TagID = t.ID
    GROUP BY l.ID
    HAVING COUNT(*) = 3 AND SUM(t.Name IN (?, ?, ?)) = 3;
    

    Taking out the WHERE clause allows other tags to be counted, if there are any. So the COUNT() may be greater than 3.

    Or if the count is exactly three tags, but some of these three are not the correct tags, then the SUM() condition in the HAVING clause makes sure that all three tags you want are present in the group.

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

Sidebar

Related Questions

In a MySQL database I have two tables linked in a join. One table
I have the following two tables in my mysql database. Table Name: groups id
What I have is two tables inside of a mysql database. One table contains
I have 10 tables in my database(MySQL). two of them is given below tbl_state
I have two MySQL tables, j and t, with a third normalising table, jt.
I have two tables in my MySQL database (table1 and table 2). I want
I have two tables in MySQL sales database: Orders table: CREATE TABLE salestest.`orders` (
I have two tables in my MySQL database, users and tweets, as follows: TABLE
I have in my MySQL database these two tables: CREATE TABLE IF NOT EXISTS
Suppose I have the following two tables in my MySQL database: Table 1:: EMP:

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.