Mysql连接不等于有多行

时间:2013-07-23 09:22:39

标签: mysql

我的两张桌子就像

Table 1


uid     mail


1136    xxx@gmail.com
1231    xxx@ymail.com

Table 2

uid     rid

1136    3
1136    7
1231    5
1231    2

所以我想加入这两个表,并希望得到结果uid表2中的rid不等于3。

如果您不理解我的要求,请告诉我。

2 个答案:

答案 0 :(得分: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)

答案 1 :(得分: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