我想从两个表中得到结果。表格如下: -
表1:标签 -
tagid tagname 1 science 2 technology 3 art 4 culture 5 space 6 fashion
表2: usersAndTags
tagid userid 6 23 2 97 4 23 4 97 3 56 6 23
表tags
包含tagid
和tagname
。表格userAndTags
包含tagid
和userid
。一行userAndTags
显示具有该用户标识的用户跟随该标记。
我希望表tagname
中的tagid
和tags
没有被userid
跟踪.23。什么是sql查询。
答案 0 :(得分:2)
您可以使用not exists
select *
from tags t
where not exists (select 1 from usersandtags
where t.tagid=tagid
and userid=23)
或left join
。
select t.*
from tags t
left join usersandtags u on u.tagid=t.tagid and u.userid=23
where u.tagid is null
答案 1 :(得分:2)
你可以LEFT JOIN
喜欢
select t.tagid,
t.tagname
from tags t
left join usersAndTags ut on t.tagid = ut.tagid
and ut.userid = 23
where ut.tagid is null;