不知道你所说的不行指得是什么?是报错,还是没能达到预期的目的,还是其它的。
把union改成union all试试

解决方案 »

  1.   

    不行是因为有order by a.id
    去掉order by a.id就可以
    但是我需要排序啊
      

  2.   

    select * from (
    select  a.id  id,a.name      name,a.value      value 
      from tbl a where a.flag='1' 
    union
    select '合计' id,max(a.name) name,sum(a.value) value 
      from tbl a where a.flag='1'
    ) order by 1
      

  3.   

    select 
        b.id,
        b.name,
        b.value 
    from
        (select 1 as nid,a.id,a.name,a.value 
         from tbl a 
         where a.flag='1' 
         union
         select 2,'sum',max(a.name),sum(a.value) 
         from tbl a 
         where a.flag='1') b
    order by 
        b.nid,b.id