Id Name Flag 1 张三 1 2 王五 3 3 张三 1 4 李四 2 5 张三 1 6 李四 2
我想输出的时候变成这样
Id Name Flag 1 张三 1 3 张三 1 5 张三 1 4 李四 2 6 李四 2 2 王五 2
SQL如何写?
注:只分组不统计
SQL code: select * from [TB] order by Flag
? 假如Flag是文本呢 ? select * from tb order by cast(Flag as int) o SQL code: --sinpoal
if object_id ('ta' ) is not null drop table ta create table ta(id int ,[name] char(8), flag int) insert into ta select 1,'张三',1 union all select 2,'王五',3 union all select 3,'张三',1 union all select 4,'李四',2 union all select 5,'张三',1 union all select 6,'李四',2
select * from ta order by flag
/*-----------RESULT----------- id name flag 1 张三 1 3 张三 1 5 张三 1 6 李四 2 4 李四 2 2 王五 3 */