Php 左键联接两个表,使表1中不存在表2的两列
我有两个表table1userid,regid和table2userid,hostuserid,status,其中userid或hostuserid等于myuseridalready,包含在变量$userid中。我想找到table1中的所有行,以便table1.userid=table2.userid和table1.userid=表2.1用户IDPhp 左键联接两个表,使表1中不存在表2的两列,php,mysql,Php,Mysql,我有两个表table1userid,regid和table2userid,hostuserid,status,其中userid或hostuserid等于myuseridalready,包含在变量$userid中。我想找到table1中的所有行,以便table1.userid=table2.userid和table1.userid=表2.1用户ID table1 -------- userid regid 1 gbjnknnk 2 bvgcghb 3 bjbnjb
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
)
可以在具有union的子选择上使用not in
select userid
from table1
where user1 not in (
select userid
from table2
union
select
hostuserid from table2
)
如果你想要的东西不匹配,那么你不是在寻找加入。。。我猜您需要一个子查询,使用where table1.userid not inselect userid from table2和table1.userid not in select hostuserid from table2如果您想要不匹配的内容,那么您不是在寻找联接。。。我猜您需要使用where table1.userid not inselect userid from table2和table1.userid not in select hostuserid from的子查询table2@Roasterbhusri好吧,如果我的答案是正确的,请将其标记为已接受…看看它是如何做到的。但由于我的低声誉,你不会得到任何分数。对不起,brole bhai jee le apnizindagi.@Roasterbhusri如果我的答案是正确的,请将其标记为已接受…看看这里它是如何做到的。但是由于我的名声不好,你不会得到任何分数。对不起,brole bhai jee le apni zindagi。