Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/226.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 连接两个表的问题_Php_Mysql - Fatal编程技术网

Php 连接两个表的问题

Php 连接两个表的问题,php,mysql,Php,Mysql,我有两个表user和contacts,其中register user使用csv在contacts表中添加了记录以及它们的id 我的桌子结构 使用者 接触 id name phone_no user_id 1 xyz 345 1 2 abc 123 2 结果 1) search phone_no '123' from contacts table contacts_id name phone_no user_i

我有两个表user和contacts,其中register user使用csv在contacts表中添加了记录以及它们的id

我的桌子结构

使用者

接触

id   name    phone_no   user_id
1    xyz     345        1
2    abc     123        2
结果

1) search phone_no '123' from contacts table

contacts_id   name    phone_no   user_id
     2         abc     123        2

2) i got my number '123' in 2nd row will check who has my number so got user_id='2'

3) now i have checked whether 'max' (id='2') has phone_no ('123') in contacts table

4) So max phone_no is '345'

contacts_id   name    phone_no   user_id
      1        xyz     345        1
5我得到的最大编号为“345”,另存为“xyz”,与用户_id='1'没有关系,后者是sachin


6所以我只想得到那些拥有我号码的用户,他们拥有我的号码。最终结果

我认为你需要重新设计你的模式,因为据我所知,你有一个主用户表,你有一个功能,允许用户添加其他用户作为他的联系人,因此,您需要跟踪哪些用户是其他用户的联系人

表联系人不需要保存姓名或号码。这些数据必须始终来自用户表,以确保一致性

使用者

接触

id   name    phone_no   user_id
1    xyz     345        1
2    abc     123        2
这样,您想要回答的问题将通过以下查询得到解决

select u1.name as me, 
       u2.name as contact
from   users u1 
join contacts c1 on u1.id=c1.relation_owner
join contacts c2 on c2.relation_owner=c1.relation_target and   c2.relation_target=c1.relation_owner
join users u2 on c2.relation_owner=u2.id
where u1.id=:userid
现在,如果我尝试对您的模式执行相同的操作

使用者

接触

id   name    phone_no   user_id
1    xyz     345        1
2    abc     123        2
在澄清后编辑

获取用户1 sachin的联系人

select u1.name as me, 
       u2.name as contact 
from user u1 
join contacts c1 on u1.phone_no = c1.phone_no
join user u2 on c1.user_id=u2.id
where user.id=1
获取用户sachin的联系人,而用户sachin的联系人中又包含sachin

select u1.name as me, 
       u2.name as contact 
from user u1 
join contacts c1 on u1.phone_no = c1.phone_no
join user u2 on c1.user_id=u2.id
join contacts c2 on u2.phone_no = c2.phone_no and c2.user_id=u1.id
where u1.id=1

我建议在contacts.phone\u no上创建一个索引,并在users.phone\u no上创建一个唯一索引,以增强您的查询。

联系人表的用途是什么?它不能只连接用户,因为他们已经保存了手机。\u否?所以结构应该是id,user_id1,user_id2,你能提供想要的结果吗?您想做什么并不明显,因为表结构似乎很神秘。表中的人也会联系用户吗?如果是,除了名称之外,还有什么方法可以识别他们吗?我这里缺少一个联系人id,请显示您的尝试,不要期望我们编写一个免费的、随时可用的解决方案。我已经简要提供了代码和实际需求。希望你能帮上忙。提前感谢!!!我在问题中提到,注册用户将使用csv上传他们的联系人。因此,我将他们的用户id附加到这些联系人上。所以我不能像你的例子那样修改联系人。我很欣赏你的例子,但与我的结果不符。请检查我的代码,只有最后一次连接给了我原始结果。好的,所以我的猜测似乎是正确的。联系人表上的用户id是关系所有者,而电话号码是在用户表中查找关系目标的关键。我正面临着同样的问题。我得到了拥有我的号码的列表,但当我运行查询时,它显示模式不明确。这就是问题所在。请帮助我。我编辑的答案中的最后一个查询应该运行正常。您可以在示例中添加更多行吗?这也将有助于我更新我的问题。请检查一下,希望你能明白。我到底需要什么。并回复类似于我的代码。
id   name    phone_no   user_id
1    xyz     345        1
2    abc     123        2
select u1.name as me, 
       u2.name as contact 
from user u1 
join contacts c1 on u1.phone_no = c1.phone_no
join user u2 on c1.user_id=u2.id
where user.id=1
select u1.name as me, 
       u2.name as contact 
from user u1 
join contacts c1 on u1.phone_no = c1.phone_no
join user u2 on c1.user_id=u2.id
join contacts c2 on u2.phone_no = c2.phone_no and c2.user_id=u1.id
where u1.id=1