【问题标题】:left join two tables such that two columns of table 2 does not exist in table1左连接两个表,使得表 2 的两列在 table1 中不存在
【发布时间】:2017-08-24 16:20:30
【问题描述】:

我有两个表 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)

【问题讨论】:

  • 如果你想要不匹配的东西,那么你不是在寻找连接......我猜你想要一个使用where table1.userid not in(select userid from table2) and table1.userid not in (select hostuserid from table2)的子查询

标签: php mysql


【解决方案1】:

您可以在带有联合的子选择中使用 not in

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

【讨论】:

  • @Roasterbhusri 好吧,如果我的回答是正确的,请将其标记为已接受...看看这里如何meta.stackexchange.com/questions/5234/…
  • 它已经做到了。但是由于我的声誉低,你不会得到任何积分。对不起兄弟
  • le bhai jee le apni zindagi.
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-09-25
  • 1970-01-01
  • 1970-01-01
  • 2013-10-20
相关资源
最近更新 更多