SELECT split.share, split.weight, split.creditor, split.debtor, share.amount, wsum.sum
FROM (
SELECT split.share, split.weight, split.creditor, split.debtor, share.amount
FROM split
LEFT JOIN share
ON split.share = share.id
WHERE debtor = 6 OR creditor = 6
) As split
LEFT JOIN (
SELECT split.share, SUM(weight) AS sum
FROM split
GROUP BY split.share
) wsum
ON split.share = wsum.share;
或更正确地书写:
SELECT split.share, split.weight, split.creditor, split.debtor, share.amount, wsum.sum
FROM split
LEFT JOIN share
ON split.share = share.id
LEFT JOIN (
SELECT split.share, SUM(weight) AS sum
FROM split
GROUP BY split.share
) wsum
ON split.share = wsum.share
WHERE split.debtor = 6 OR split.reditor = 6;