mysql连接电话连接客户端电话

mysql连接电话连接客户端电话,mysql,Mysql,我有一张叫phonecalls的桌子 call_id(int,AA),number varchar(50),calldate varchar(50) 还有一个叫客户 具有 id(int,AA),clientname,phone1,phone2,phone3 客户端表中可能有重复的客户端名称和电话 如何显示所有电话记录,以及号码是否与3个电话号码字段中的任何一个匹配 EAX示例数据: 电话 客户 如何获得以下结果: 1, 12345, 1/1/2016, 1, John Doe, 0010 123

我有一张叫phonecalls的桌子 call_id(int,AA),number varchar(50),calldate varchar(50) 还有一个叫客户 具有 id(int,AA),clientname,phone1,phone2,phone3

客户端表中可能有重复的客户端名称和电话

如何显示所有电话记录,以及号码是否与3个电话号码字段中的任何一个匹配

EAX示例数据:

电话 客户 如何获得以下结果:

1, 12345, 1/1/2016, 1, John Doe, 0010 123456, 001033333, null
2, 5555, 2/2/2016, 2, Joan Doe, null, 55557, null (only the first one found matching one of their phones) 
3, 55551. 3/1/2016, null, nulll, null, null  (not found)
4, 888, 8/1/2016, 5. Alan Doe, 2222, 66666, 8888
谢谢

  • 左连接
    以保留“未找到”案例
  • a.number=left(b.phone1,length(a.number))
    以前缀连接
  • 在电话1,2,3上匹配
  • 按a.call\u id分组
    每次通话只保留一行
查询:

select a.*, b.*
from phonecalls a
left join clients b on instr(b.phone1, a.number) 
                    or instr(b.phone2, a.number)
                    or instr(b.phone3, a.number) 
group by a.call_id;

888和8888是如何匹配的?nvm,我看你是通过前缀匹配的谢谢。只有当a.number等于b.phoneX时,它才会返回包含phonecall号码一部分的客户端电话号码。例如,如果我有a.number=888和客户端a.phone=28883,则返回空字段。你对此有什么建议吗?我试过下面的方法,但也不管用。从phonecalls中选择c.*,p.*,p.*,p LEFT以c ON(c.phone1类CONCAT(“%”,p.number,“%”)或c.phone2类CONCAT(“%”,p.number,“%”)或c.phone3类CONCAT(“%”,p.number,“%”)按p排序加入客户端。
call id
?谢谢。@WilliamBird,
instr
应该可以完成这项工作。请参阅更新的答案。谢谢,它成功了。我试着用LIKE而不是INSTR来解决它。
1, 12345, 1/1/2016, 1, John Doe, 0010 123456, 001033333, null
2, 5555, 2/2/2016, 2, Joan Doe, null, 55557, null (only the first one found matching one of their phones) 
3, 55551. 3/1/2016, null, nulll, null, null  (not found)
4, 888, 8/1/2016, 5. Alan Doe, 2222, 66666, 8888
select a.*, b.*
from phonecalls a
left join clients b on instr(b.phone1, a.number) 
                    or instr(b.phone2, a.number)
                    or instr(b.phone3, a.number) 
group by a.call_id;