2016-12-14 26 views
0

我想要两个表的结果。表如下: -不要在第二个表中映射列数据的表中选择该行

表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表包含tagidtagname。表userAndTags包含tagiduserid。一排userAndTags向用户显示具有该用户标识的用户。

我想从表tagstagnametagid这是不是跟着userid 23.什么是sql查询。

回答

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 
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; 
相关问题