簡體   English   中英

三張桌子,兩個聯接,結果只需要一張桌子

[英]Three Tables, Two Joins, Only One Table Needed For Results

我有三個表:“客戶”,“服務”和“客戶位置”。 我正在運行一個查詢,該查詢需要返回接收某種服務的客戶端的位置。 因此,請使用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

因此,這給了我艾比住在波士頓的三倍,而不是理想的一次。

現在,我確切知道為什么會這樣。 服務表中使用的LEFT JOIN用於結果,而Abby的三次咨詢會導致該城市重復三遍。

還有另一種方法可以做到這一點,以便服務表不會引起這樣的重復嗎? 我嘗試了INNER JOIN並得到了同樣的東西。

要么使用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

示例SQL提琴

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 ,但此方法應該會更好。 無需僅在另一步驟中將其刪除即可生成重復的結果。

您可以獲取該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

您正在匯總詳細結果集。 使用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

您只需使用一個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'

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM