合并相似的行并将它们的值相加?

时间:2011-10-04 05:42:48

标签: mysql

这是我的表:

URL           | Art | Design 
example1.com     1             
example1.com     1             
example1.com     1             
example1.com            1         
example2.com            1
example2.com            1

我想合并具有相同网址的列,并在此过程中对艺术和设计的值求和,得到类似的内容:

URL           | Art | Design
example1.com     3      1
example2.com            2

这是怎么做到的?

5 个答案:

答案 0 :(得分:3)

使用GROUP BY和SUM

SELECT URL, SUM(Art) as Art, SUM(Design) AS Design
FROM yourtable
GROUP BY URL

答案 1 :(得分:1)

select URL,sum(art),sum(design) From MYTABLE group by URl

答案 2 :(得分:1)

select url,SUM(art),SUM(design) from  tab1 group by url

其中tab1是您的表名

答案 3 :(得分:0)

试试这个:

SELECT URL, COUNT(Art), COUNT(Design)
FROM myTable
GROUP BY URL

答案 4 :(得分:0)

你到现在为止做过什么尝试吗?

这很简单:

select url, SUM(art) as Art, SUM(design) as Design from [MyTable] group by url