繁体   English   中英

左联接两个表,以使表1中不存在表2的两列

[英]left join two tables such that two columns of table 2 does not exist in table1

我有两个表table1(userid,regid)和table2(userid,hostuserid,status),其中userid或hostuserid等于myuserid(已经包含在$ userid变量中)。我想找到table1中的所有行,例如table1.userid! = table2.userid和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)

您可以在带有union的子选择上使用not in

select userid 
from table1 
where user1 not in ( 
      select userid 
      from table2 
      union 
      select 
      hostuserid from table2
)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM