我有两个表table1(userid,regid)和table2(userid,hostuserid,status),其中userid或hostuserid等于myuserid(已包含在变量$ userid中)。我想查找table1中的所有行,使得table1 .userid!= table2.userid AND table1.userid!= table2.userid。
table1
--------
userid regid
1 gbjnknnk
2 bvgcghb
3 bjbnjb
table2
-------
userid hostuserid
1 5
5 2
$userid=5
查询应该只返回一行=> 3 bjbnjb 如何实现相同。 我曾尝试过什么
SELECT * FROM table1 JOIN table2 WHERE (table1.userid!=table2.userid AND
table2.hostuserid=$userid) AND (table1.userid!=table2.hostuserid AND
table2.userid=$userid)
答案 0 :(得分:0)
您可以在带有联合
的子选择中使用not inselect userid
from table1
where user1 not in (
select userid
from table2
union
select
hostuserid from table2
)