您的位置:首页 > 其它

看了群友的帖子,加深对相关子查询的理解!!!

2006-09-29 10:47 423 查看
create table test (CPBH char(7),LSH int default 0)
insert test(CPBH) select '6666122'
insert test(CPBH) select '6666122'
insert test(CPBH) select '6666122'
insert test(CPBH) select '6666133'
insert test(CPBH) select '6666133'
insert test(CPBH) select '5454465'
insert test(CPBH) select '5451165'
insert test(CPBH) select '4541165'
insert test(CPBH) select '4541165'
insert test(CPBH) select '5555511'
--select * from test

alter table test
add id int identity(1,1)
go

update test set
LSH=
(
select count(*)+1 from test where cpbh=a.cpbh and id<a.id ----内层表为引用表,因为肯定有一条记录不符合id<a.id, 所以0,1,2
) from test a ------外层表

alter table test
drop column id
go

select * from test

drop table test
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: