0
我有一個MySQL數據庫下表設置:選擇每個客戶的名字和時間,他們都在會議花費的總金額
Client:
id
name
Session:
id
service_id
date
Attendance:
id
client_id
session_id
Service:
id
duration
客戶端有一個多對多的關係,通過考勤表會話。
我想編寫一個查詢將返回每個客戶的名字和他們在會議已經花的時間總量,這樣的:
Name Total Time (Mins)
Person A 200
Person B 500
Person C 100
到目前爲止,我的查詢看起來是這樣的:
SELECT
(SELECT SUM(services.duration)
FROM services
INNER JOIN sessions
ON sessions.service_id = services.id
INNER JOIN attendances
ON attendances.session_id = session.id
INNER JOIN clients
ON clients.id = attendances.client_id
GROUP BY clients.id) AS total_duration,
client.name
FROM clients
但是,這不會返回任何結果。然而,如果我單獨運行查詢的任一部分,我得到我所需要的數據,即:
SELECT name FROM clients
OR
SELECT SUM(services.duration)
FROM services
INNER JOIN sessions
ON sessions.service_id = services.id
INNER JOIN attendances
ON attendances.session_id = session.id
INNER JOIN clients
ON clients.id = attendances.client_id
GROUP BY clients.id
有人能看到一個原因,當我結合它不會工作他們倆?