三个表,两个联接,只有一个表需要结果

时间:2015-02-11 13:47:39

标签: mysql sql join

我有三个表:客户端,服务和客户端位置。我正在运行一个查询,需要返回收到某个服务的客户端的位置。所以使用SELECT中的第二个表和WHERE中的第三个表。我正在使用两个LEFT JOIN并以不合需要的方式重复我的结果。

以下是三个表格的简化版本......

客户(客户)

id_client | clientName
----------------------
1         | Abby
2         | Betty
3         | Cathy

客户服务(服务)仅在WHERE语句中使用

id_client | date      | serviceType
-----------------------------------
1         | 1/5/2015  | Counseling
1         | 1/12/2015 | Counseling
1         | 1/19/2015 | Counseling
2         | 1/21/2015 | Sup. Group

客户端位置(位置)仅在SELECT语句中使用

id_client | city
----------------------
1         | Boston, MA
3         | Providence, RI

以下是查询

SELECT clients.clientName,locations.city
FROM clients
LEFT JOIN locations ON clients.id_client=locations.id_client
LEFT JOIN services ON clients.id_client=services.id_client
WHERE services.serviceType='Counseling'

结果

clientName | city
-----------------------
Abby       | Boston, MA
Abby       | Boston, MA
Abby       | Boston, MA

所以它给了我Abby住在波士顿三次,而不是想要的。

现在,我确切地知道为什么会这样。用于服务表的LEFT JOIN用于结果,Abby的三个咨询会议导致该城市重复三次。

有没有其他方法来执行此JOIN,以便服务表不会导致这样的重复?我尝试过INNER JOIN并得到同样的东西。

5 个答案:

答案 0 :(得分:2)

使用distinct

SELECT DISTINCT clients.clientName,locations.city
FROM clients
LEFT JOIN locations ON clients.id_client=locations.id_client
LEFT JOIN services ON clients.id_client=services.id_client
WHERE services.serviceType='Counseling'

group by

SELECT clients.clientName,locations.city
FROM clients
LEFT JOIN locations ON clients.id_client=locations.id_client
LEFT JOIN services ON clients.id_client=services.id_client
WHERE services.serviceType='Counseling'
GROUP BY clients.clientName,locations.city

或子查询

SELECT clients.clientName,locations.city
FROM clients
LEFT JOIN locations ON clients.id_client=locations.id_client
LEFT JOIN (
  SELECT id_client, serviceType 
  FROM services 
  GROUP BY id_client, serviceType 
) services ON clients.id_client=services.id_client
WHERE services.serviceType='Counseling'
GROUP BY clients.clientName,locations.city

Sample SQL Fiddle

答案 1 :(得分:2)

使用exists

SELECT c.clientName, l.city
FROM clients c JOIN
     locations l
     ON c.id_client = l.id_client
WHERE EXISTS (SELECT 1
              FROM services s
              WHERE c.id_client = s.id_client AND
                    s.serviceType = 'Counseling'
             );

虽然您可以使用group bydistinct,但此方法的效果会更好。没有必要生成重复的结果只是为了在另一个步骤中删除它们。

答案 2 :(得分:1)

您可以获取该serviceType的不同客户端ID,然后加入客户端和位置以获取有关客户端的更多详细信息。

SELECT clients.clientName,locations.city
FROM 
(Select distinct id_client from services WHERE services.serviceType='Counseling') s
INNER JOIN clients ON clients.id_client = s.id_client
LEFT JOIN locations ON clients.id_client=locations.id_client

答案 3 :(得分:0)

您正在总结详细结果集。所以使用DISTINCT做到这一点。

SELECT DISTINCT clients.clientName, locations.city
  FROM clients
  LEFT JOIN locations ON clients.id_client=locations.id_client
  LEFT JOIN services ON clients.id_client=services.id_client
 WHERE services.serviceType='Counseling'

或者使用GROUP BY查询并提供一些摘要统计信息:

SELECT DISTINCT clients.clientName, locations.city,
       COUNT(*) service_count
  FROM clients
  LEFT JOIN locations ON clients.id_client=locations.id_client
  LEFT JOIN services ON clients.id_client=services.id_client
 WHERE services.serviceType='Counseling' 
 GROUP BY clients.clientName, locations.city

答案 4 :(得分:0)

您只需使用distinct子句即可避免获得双重结果。

SELECT distinct clients.clientName,locations.city
FROM clients
LEFT JOIN locations ON clients.id_client=locations.id_client
LEFT JOIN services ON clients.id_client=services.id_client
WHERE services.serviceType='Counseling'