[英]CodeIgniter Two tables Left Join does not return Join Condition id of left table when miss in right table
[英]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.