MYSQL - 如何让3行数据成为1行数据

时间:2014-04-24 12:00:30

标签: mysql sql

现在我联合3表并选择下面显示。

|remark|totalA|totalB|tatalC|
-----------------------------
|   a  | 123  | NULL | NULL |

|   a  | NULL | 123  | NULL |

|   a  | NULL | NULL | 123  |

如何让它成为如下所示的1行?

|remark|totalA|totalB|tatalC|
-----------------------------
|   a  | 123  | 123  | 123  |

我尝试分组并将其区分开,但它显示在下面

|remark|totalA|totalB|tatalC|
-----------------------------
|   a  | 123  | NULL | NULL |

3 个答案:

答案 0 :(得分:2)

select 
   remark,
   sum(totalA) as totalA,
   sum(totalB) as totalB,
   sum(totalC) as totalC
from the_table
group by remark

答案 1 :(得分:2)

sumgroup by一起使用

select remark, sum(totalA), sum(totalB), sum(totalC)
from table_name
group by remark

答案 2 :(得分:1)

使用聚合函数,如MIN(),默认情况下将排除空值:

SELECT remark, MIN(totalA), MIN(totalB), MIN(totalC)
FROM Remarks
GROUP BY remark