2013-07-23 11 views
0

我的兩個表都像mysql用不等於加入到具有多行

Table 1 


uid  mail 


1136 [email protected] 
1231 [email protected] 

而且

Table 2 

uid  rid 

1136 3 
1136 7 
1231 5 
1231 2 

所以我想加入這兩個表,並希望得到的結果UID其中去掉表2不等於3.

如果您不瞭解我的要求,請讓我知道。

+0

那麼什麼是你期望的輸出?你有什麼嘗試? – hims056

+0

SELECT mail FROM users LEFT JOIN users_roles ON(users.uid = users_roles.uid AND users_roles.rid <>'3')。但儘管1136已經擺脫了3,但它依然存在。我不想那 – pravat231

回答

2
select t1.uid, mail, rid 
from table1 t1 
join table2 t2 
on t1.uid = t2.uid and t2.uid not in (select uid from table2 where rid = 3) 
+0

這是不正確的,因爲這個查詢也將返回1136已經擺脫了3.但我不想這樣。我只想要1231,因爲那裏沒有擺脫3。 – pravat231

+0

@ pravat231我在閱讀你的評論後更新了。 –

+0

是的,我可以看到讓我檢查。謝謝 – pravat231

0
select t1.uid, mail, group_concat(rid) as rids 
from table1 t1 
left join table2 t2 
on t1.uid = t2.uid 
group by t1.uid 
having not find_in_set(3,rids) 
; 

fiddle