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!
There are two ways to do this. I prefer the first way, which is to self-join for each tag:
The other way also works, but using
GROUP BY
in MySQL tends to incur a temporary table and performance is slow:Re comment from @Erikoenig:
If you want to make sure there are no extra tags, you can do it this way:
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.
You need locations where there doesn't exist a given tag that doesn't appear in the LocationsTagsAssoc table with the location.
You can specify the given tags with IN () as in the following, or by joining onto another table containing them.
I.e.