问题如下:
A  B  C
1  1  1
1  1  2
1  1  3
1  1  4对具有上面性质的信息进行查询,并把C的所有值相加,并删除多余信息。
查询后形式变为:
A  B  C
1  1  10

解决方案 »

  1.   

    select a,b,sum(c)
    from tb
    group b a,b
      

  2.   

    -- 先创建临时表
    create table tmp
    as select * from tb where 1=2;insert into tmp
    select a, b, sum(c) as c
    from tb;delete from tb
    where exists(select 1 from tmp where tmp.a=tb.a and tmp.b=tb.b);insert into tb
    select a,b,c from tmp;-- 如果你的表实时业务非常繁忙的话,上面的方法不太适用
    -- 建议用另外的方法!
      

  3.   

    select a,b,sum(c)
    from tt group by a,b
      

  4.   

    没办法用一句SQL语句来完成。 如果你的数据几百万,同考虑下面的方法。1. create table xxx as select a,b,sum(c) from tb group b a,b
    2. drop table tb
    3. RENAME TABLE xxx TO tb但前提是你的原因是简单表,没有什么外键参照。