加上order by后语法是通不过的。如何既排序又UNION ALL?
declare @tb table (id nvarchar(10))
insert @tb
select 1 union all
select 2select id from @tb --order by id desc
union all
select 'a'
目标结果
id
2
1
a
declare @tb table (id nvarchar(10))
insert @tb
select 1 union all
select 2select id from @tb --order by id desc
union all
select 'a'
目标结果
id
2
1
a
union all
select 'a'
select id from @tbunion all
select 'a'
order by id desc
--union后order by字段放最后的
select id from @tb
union all
select 'a'
order by id desc
select ltrim(id) as id from @tb
union all
select 'a'
order by 1 desc
(select id from @tb order by id desc) t
union all select 'a'
select id from @tb
union all
select 'a'
) order by id desc
insert @tb
select 1 union all
select 2 SELECT id FROM
(
select orderField = 0, id from @tb
union ALL
select orderField = 1, 'a'
) t
order by orderField, id desc
/*
id
2
1
a
*/
1.语法都通不过。
2.我这里只是用1,2,a举例,不要只为例子做文章,要考虑其他场景,例如1,B,2,+A。
3.根本不符合目标结果。谢谢各位的参与。