您还没有给出ClientBank
andBank
表的结构。我认为您插入了类似这样的记录:
INSERT INTO `ClientBank`
(`ClientID`, `FName`, `Lname`)
VALUES
(1, 'Abraham', 'Backman'),
(2, 'Carl', 'Dacosta'),
(3, 'Erwin', 'Fabio'),
(4, 'Gabriel', 'Haddon');
INSERT INTO `Bank`
(`DepositID`, `ClientID`, `Deposit`)
VALUES
(1, 1, 100),
(2, 2, 200),
(3, 3, 300),
(4, 4, 400),
(5, 2, 500),
(6, 3, 600);
您可以通过以下方式获得每个客户的存款总额:
SELECT
client.LName, client.FName, SUM(b.Deposit) as Total
FROM
ClientBank client
INNER JOIN
Bank b ON (client.ClientID = b.ClientID)
GROUP By
client.LName, client.FName;
如果您只需要特定客户端(使用其名称而不是客户端 ID 进行过滤):
SELECT
client.LName, client.FName, SUM(b.Deposit) as Total
FROM
ClientBank client
INNER JOIN
Bank b ON (client.ClientID = b.ClientID)
WHERE
client.LName = 'Fabio' AND client.FName = 'Erwin'
GROUP By
client.LName, client.FName;