我试图从共享相似数据的所有行中获取单个列的总和。例如,给定以下数据:

|ppID | cID   | Count | NameSpace|
|-----|-------|-------|----------|
|586  | 18281 | 1     | LAB      |
|587  | 18269 | 1     | LAB      |
|588  | 18281 | 1     | LAB      |
|589  | 17823 | 1     | IPB      |
|590  | 18281 | 1     | LAB      |
|591  | 18256 | 1     | LAB      |
|592  | 18256 | 1     | LAB      |
|593  | 18269 | 1     | IPB      |
|-----|-------|-------|----------|


我希望得到:

|ppID | cID   | Count | NameSpace|
|-----|-------|-------|----------|
|586  | 18281 | 3     | LAB      |
|587  | 18269 | 1     | LAB      |
|589  | 17823 | 1     | IPB      |
|591  | 18256 | 2     | LAB      |
|593  | 18269 | 1     | IPB      |
|-----|-------|-------|----------|


我拼凑了几件不同的东西,并想出了`

SELECT * FROM PopularPages
WHERE cID IN (SELECT cID FROM PopularPages
              GROUP BY cID
              HAVING COUNT(cID) > 1)
ORDER BY cID, Namespace


它将列出每一行,但不计算Count列的总和。任何帮助,将不胜感激。

最佳答案

这是你想要的吗 ?

SELECT
  MIN(ppID) as ppID,
  cID,
  SUM(`Count`) as COUNT,
  NameSpace
FROM PopularPages
GROUP BY cID
HAVING Count > 1;

关于mysql - 多行具有相同值的列的总和,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/35048302/

10-13 01:37