34,594
社区成员
发帖
与我相关
我的任务
分享
Select [tid],[tname],[tcount1] from tb where [tcount1] is not null
union all
Select [tid],[tname],[tcount2] from tb where [tcount2] is not null
order by tid
if not object_id('tb') is null
drop table tb
Go
Create table tb([tid] int,[tname] nvarchar(2),[tcount1] int,[tcount2] int)
Insert tb
select 1,N'奔驰',1,2 union all
select 2,N'宝马',2,3 union all
select 3,N'福特',5,null union all
select 4,N'本田',null,5
Go
Select [tid],[tname],[tcount1] from tb where [tcount1] is not null
union all
Select [tid],[tname],[tcount2] from tb where [tcount2] is not null
order by tid
/*
tid tname tcount1
----------- ----- -----------
1 奔驰 1
1 奔驰 2
2 宝马 3
2 宝马 2
3 福特 5
4 本田 5
(6 個資料列受到影響)
*/