我试图将两个表链接在一起,以便可以从中获取以下数据。但是,我得到的只是将总数加到第一人称,而对于其他人则一无所有。
它看起来应该像下面这样

pid firstname nid  Total
18  Teresa     3  65846.50
18  Richard    1  3005.00
18  Camdon     7  2000.00


联接有效,但是总和不能正常工作

SELECT
    notarys.pid,
    notarys.firstname,
    notarys.nid,
    SUM(
    signings.samount+
    signings.prtamount+
    signings.faxamount
    )AS Total
    FROM
        notarys
    INNER JOIN signings ON notarys.nid = signings.nid
    WHERE
        signings.pid = signings.pid
    AND signings.done = 1
    ORDER BY
    notarys.nid ASC

最佳答案

需要一个GROUP BY

SELECT
    notarys.pid,
    notarys.firstname,
    notarys.nid,
    SUM(
    signings.samount+
    signings.prtamount+
    signings.faxamount
    )AS Total
FROM
    notarys
INNER JOIN signings ON notarys.nid = signings.nid
WHERE
    signings.pid = signings.pid
AND signings.done = 1
GROUP BY notarys.nid, notarys.pid
ORDER BY notarys.nid ASC

关于mysql - 内部Join中三列的总和,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/36190377/

10-11 07:44