2016-12-14 54 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; 
相關問題