簡體   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