基本上,当某个值(在我的情况下为COMMUNICATIONS_ID)相等时,我试图对计算字段的值求和。这些分数与相同的COMMUNICATIONS_ID相关联,我想对这些值进行汇总。

我是SQL新手,这是我误入歧途的第一次尝试:

SELECT *
FROM consumer_action_log as cal1
JOIN consumer_action_log as cal2
    ON cal1.COMMUNICATIONS_ID=cal2.COMMUNICATIONS_ID
    AND cal1.COMM_TYPE_ID=4

最佳答案

SELECT COMMUNICATIONS_ID, SUM(fieldName)
FROM consumer_action_log
WHERE COMM_TYPE_ID = 4
GROUP BY COMMUNICATIONS_ID


我认为不需要在这里将表加入自身。

关于sql - 汇总列值相同的MySQL计算字段,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/4810807/

10-09 05:13