Getting related tags from SQL Server when you have filtered down

前端 未结 3 1864
春和景丽
春和景丽 2021-02-06 18:47

I asked this question before and got a great working answer.

what is the query to get "related tags" like in stack overflow

but i realized that SOF ac

3条回答
  •  执笔经年
    2021-02-06 19:09

    Let's say you have a table called entity_tags linking entities with the tags like this :

    entity_id : INTEGER
    tag_id    : INTEGER
    

    Let's say you user as selected N tags @1, @2, ... @N. To get the other tags associated with the results (and as a bonus, their frequency of occurence):

    SELECT et.tag_id, COUNT(et.entity_id) as frequency FROM
     entity_tags AS et
     JOIN entity_tags AS et1 ON (et1.entity_id=et.entity_id AND et1.tag_id=@1)
     ...
     JOIN entity_tags AS etN ON (etN.entity_id=et.entity_id AND etN.tag_id=@N)
    WHERE et.tag_id NOT IN (@1, @2, ... @N)
    GROUP BY et.tag_id;
    

提交回复
热议问题