Select in a many-to-many relationship in MySQL

后端 未结 2 1027
天命终不由人
天命终不由人 2021-01-06 02:38

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 relationshi

相关标签:
2条回答
  • 2021-01-06 03:08

    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 讨论(0)
  • 2021-01-06 03:17

    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.

    SELECT l.*
    FROM Locations AS l
    WHERE NOT EXISTS (
        SELECT NULL FROM Tags AS t
        WHERE NOT EXISTS (
            SELECT NULL FROM LocationsTagsAssoc AS lt
            WHERE lt.LocationId = l.ID
                AND lt.TagID = t.ID
        )
            AND t.ID IN (1, 2, 3,...)
    )
    
    0 讨论(0)
提交回复
热议问题