MySQL分组-联合所有
我对报告有以下查询:MySQL分组-联合所有,mysql,union-all,Mysql,Union All,我对报告有以下查询: select @t := '' as 'Clave', @tf:='Inventario Físico' as 'Descripción', @t:= '' as 'Cantidad', @t:= '' as 'Precio Unitario' union all select @t:= '', @t:= '', @t:= '', @t:= '' union all (select cla, des, can, CAST(pl1*can as Decimal(10,2)
select @t := '' as 'Clave', @tf:='Inventario Físico' as 'Descripción', @t:= '' as 'Cantidad', @t:= '' as 'Precio Unitario' union all
select @t:= '', @t:= '', @t:= '', @t:= '' union all
(select cla, des, can, CAST(pl1*can as Decimal(10,2)) from inventario order by cla) union all
select @t:= '', @t:='', @tnde := 'Número de Elementos: ', count(*) from inventario union all
select @t:= '', @t:= '', @tne:= 'Suma total: $', sum(ppu) from inventario;
我需要第三个查询的“订购人”
select cla, des, can, CAST(pl1*can as Decimal(10,2)) from inventario order by cla
就其本身而言,这行代码工作得非常完美,但是,当它位于联合体之间时,所有的信息都不是有序的。我怎样才能解决这个问题?谢谢。
union all
不保证数据符合子查询指定的顺序。您需要执行一个显式的orderby来获得该结果
这种方法添加了一个排序列以将组保持在一起。最后的orderby
子句首先按排序
排序,然后按用于排序第三个子查询的列排序:
(select @t := '' as Clave, @tf:='Inventario Físico' as Descripción,
@t:= '' as "Cantida", @t:= '' as "Precio Unitario", 0 as ordering
) union all
(select @t:= '', @t:= '', @t:= '', @t:= '', 1 as ordering) union all
(select cla, des, can, CAST(pl1*can as Decimal(10,2)), 2 from inventario) union all
(select @t:= '', @t:='', @tnde := 'Número de Elementos: ', count(*), 3 from inventario) union all
(select @t:= '', @t:= '', @tne:= 'Suma total: $', sum(ppu), 4 from inventario)
order by ordering, clave;
我还将列别名上的单引号更改为双引号。我认为对字符串常量只使用单引号是一种很好的做法