用于从

时间:2016-02-04 03:45:21

标签: sql

我有标准标签模型使用三个表方法... Object,ObjectTag,Tag。所以一个Object可以有多个标签。

现在,假设我有一个查询,从给定的Tag ='html'中选择对象:

SELECT o.* 
FROM Object o 
INNER JOIN ObjectTag ot ON ot.objectId = o.Id
INNER JOIN Tag t ON t.id = ot.tagId
WHERE t.name = 'html'
ORDER BY o.Id

那么我该如何使用此查询或结果来获取相关标签。含义我想获得每个对象关联的所有其他标记。所以想象一下我从上面的查询得到以下结果:

Object 1 ---> [html, apple]
Object 2 ---> [html, web, standard]
Object 3 ---> [html, coding]

所以我想获得此查询中对象的所有其他标记:

[apple, web, standard, coding]

我似乎无法理解这一点。

1 个答案:

答案 0 :(得分:0)

select distinct t.name
from ObjectTag ot join
     tags t
     on ot.tagId = t.id
where exists (select 1
              from ObjectTag ot2 join
                   Tags t2
                   on ot2.tagid = t2.id
              where ot.objectId = ot2.objectId and
                    t2.name = 'html' and t2.name <> t.name
             ) ;
相关问题