我希望能够根据电话号码查找公司名称。
我需要一个SQL查询,允许我查找可能是联系电话号码或公司电话号码的电话号码,并返回公司名称。
我的表结构(简化)
+----+--------+-----------+-----------+
| id | Name | Phone | CompanyID |
+----+--------+-----------+-----------+
| 1 | John | 258649864 | 3 |
| 2 | Martin | 258699182 | 2 |
| 3 | Amy | 296847348 | 2 |
+----+--------+-----------+-----------+
+----+----------------------+------------+
| id | Name | Phone |
+----+----------------------+------------+
| 1 | Virtual Company Inc. | 7070305030 |
| 2 | Worldwide 101 | 3050101020 |
| 3 | 24/7 Employee | 5023268293 |
+----+----------------------+------------+
我尝试过这样的事情(当试图查找数字3050101020时),但它不起作用(不返回任何行)。
SELECT Company.Name FROM Contacts
JOIN Company ON Company.id = Contacts.CompanyID
WHERE Contacts.Phone = '3050101020'
OR Company.Phone = '3050101020'
答案 0 :(得分:4)
如果有没有联系人的公司或没有指定公司的联系人,请执行FULL OUTER JOIN
:
SELECT DISTINCT Company.Name
FROM Company
FULL OUTER JOIN Contacts ON Company.id = Contacts.CompanyID
WHERE '3050101020' IN (Contacts.Phone, Company.Phone)
顺便说一句,原始查询似乎做得很好:
SQL>create table contacts (id int, name varchar(10), phone varchar(20),
SQL& companyid int);
SQL>insert into contacts values (1,'John','258649864',3);
SQL>insert into contacts values (2,'Martin','258699182',2);
SQL>insert into contacts values (3,'Amy','296847348',2);
SQL>create table company (id int, name varchar(30), phone varchar(20));
SQL>insert into company values (1,'Virtual Company Inc.','7070305030');
SQL>insert into company values (2,'Worldwide 101','3050101020');
SQL>insert into company values (3,'24/7 Employee','5023268293');
SQL>SELECT Company.Name FROM Contacts
SQL&JOIN Company ON Company.id = Contacts.CompanyID
SQL&WHERE Contacts.Phone = '3050101020'
SQL&OR Company.Phone = '3050101020';
name
==============================
Worldwide 101
Worldwide 101
2 rows found
您可以投入SELECT DISTINCT
只获取公司名称一次。 (你得到它两次因为公司有两个联系人。)
做一个LEFT JOIN
让没有联系人的公司,甚至FULL OUTER JOIN
也可以获得没有公司的联系人。
答案 1 :(得分:1)
对我来说,你的查询似乎是正确的。唯一可能的原因是没有得到结果可能是Phone列中的空白。尝试使用TRIM功能
SELECT Company.Name
FROM Contacts
JOIN Company ON Company.id = Contacts.CompanyID
WHERE RTRIM(LTRIM(Contacts.Phone)) = '3050101020'
OR RTRIM(LTRIM(Company.Phone)) = '3050101020
答案 2 :(得分:0)
我只想使用union
:
select c.name
from companies c
where c.phone = @phone
union
select c.name
from companies c join
contacts co
on co.companyid = co.id
where co.phone = @phone;
注意:这是故意使用union
删除重复项。使用两个查询会使SQL Server更有可能利用phone
上的索引(如果存在)。
如果您愿意,也可以将其写为:
select distinct c.name
from companies c left join
contacts co
on co.companyid = co.id
where co.phone = @phone or c.phone = @phone;
然而,使用or
会降低使用适当索引的可能性。
答案 3 :(得分:0)
进行存在测试以解决您的问题:
SELECT company.name
from company com
where com.phone = 'xxx'
or exists (
select 1
from contacts cnt
where cnt.phone = 'xxx' and cnt.CompanyID = com.id)
答案 4 :(得分:0)
为其编写存储过程,仅将电话号码作为参数传递 和结果公司名称
注意:将公司表作为基表,因为它有一个主键
Create procedure(@number varchar(max))
As
begin
If(Isnull(@number, 0))
BEGIN
SELECT Company.Name
FROM Company
LEFT OUTER JOIN Contacts ON Company.id = Contacts.CompanyID
WHERE Contacts.Phone = @number
OR Company.Phone = @number
END
End