例如:Shade numA 2 B 3 C 1 排序后為C 1 A 2 B 3 新的Num起點為26則新的num應為C 27 A 28 B 29
--> 测试数据:[test] if object_id('[test]') is not null drop table [test] create table [test]([Shade] varchar(1),[num] int) insert [test] select 'A',2 union all select 'B',3 union all select 'C',1--> 测试数据:[tbl] if object_id('[tbl]') is not null drop table [tbl] create table [tbl]([Shade] varchar(1),[num] int) insert [tbl] select 'C',27 union all select 'A',28 union all select 'B',29update test set num=a.num from tbl a where a.Shade=test.Shade select * from test /* Shade num ----------------- A 28 B 29 C 27 */这个你给A,B,C的值是知道的吧
if object_id('[tb]') is not null drop table [tb] go create table [tb]([Shade] varchar(1),[num] int) insert [tb] select 'A',2 union all select 'B',3 union all select 'C',1 godeclare @num int select @num=26update t set num=(select count(1)+1 from tb where num<t.num)+@num from tb tselect * from tb order by num /** Shade num ----- ----------- C 27 A 28 B 29(3 行受影响) **/
樹人同志,請問這句(select count(1)+1 from tb where num<t.num)+@num 怎樣解釋呢?
B 3
C 1
排序后為C 1
A 2
B 3
新的Num起點為26則新的num應為C 27
A 28
B 29
--> 测试数据:[test]
if object_id('[test]') is not null drop table [test]
create table [test]([Shade] varchar(1),[num] int)
insert [test]
select 'A',2 union all
select 'B',3 union all
select 'C',1--> 测试数据:[tbl]
if object_id('[tbl]') is not null drop table [tbl]
create table [tbl]([Shade] varchar(1),[num] int)
insert [tbl]
select 'C',27 union all
select 'A',28 union all
select 'B',29update test
set num=a.num from tbl a where a.Shade=test.Shade
select * from test
/*
Shade num
-----------------
A 28
B 29
C 27
*/这个你给A,B,C的值是知道的吧
go
create table [tb]([Shade] varchar(1),[num] int)
insert [tb]
select 'A',2 union all
select 'B',3 union all
select 'C',1
godeclare @num int
select @num=26update t
set num=(select count(1)+1 from tb where num<t.num)+@num
from tb tselect * from tb order by num
/**
Shade num
----- -----------
C 27
A 28
B 29(3 行受影响)
**/
樹人同志,請問這句(select count(1)+1 from tb where num<t.num)+@num 怎樣解釋呢?