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
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
去掉order by a.id就可以
但是我需要排序啊
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
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