MySQL / PHP: Find similar / related items by tag / taxonomy

后端 未结 5 539
太阳男子
太阳男子 2020-12-29 08:37

I have a cities table which looks like this.

|id| Name    |
|1 | Paris   |
|2 | London  |
|3 | New York|

I have a tags table which looks li

5条回答
  •  盖世英雄少女心
    2020-12-29 09:03

    Could this be a push in the right direction?

    SELECT cities.name, ( 
                        SELECT cities.id FROM cities
                        JOIN cities_tags ON cities.id=cities_tags.city_id
                        WHERE tags.id IN(
                                         SELECT cities_tags.tag_id
                                         FROM cites_tags
                                         WHERE cities_tags.city_id=cites.id
                                         )
                        GROUP BY cities.id
                        HAVING count(*) > 0
                        ) as matchCount 
    FROM cities
    HAVING matchCount >0
    

    What I tried was this:

    // Find the citynames:
    Get city.names (SUBQUERY) as matchCount FROM cities WHERE matchCount >0

    // the subquery:
    select the amount of tags cities have which (SUBSUBQUERY) also has

    // the subsubquery
    select the id of the tags the original name has

提交回复
热议问题