看了群友的帖子,加深对相关子查询的理解!!!
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
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
相关文章推荐
- 从一个MySQL left join优化的例子加深对查询计划的理解
- 从一个MySQL left join优化的例子加深对查询计划的理解
- 从一个MySQL left join优化的例子加深对查询计划的理解
- 相关子查询中exists后select 加数字的理解
- 从一个MySQL left join优化的例子加深对查询计划的理解
- 从一个MySQL left join优化的例子加深对查询计划的理解
- 教你理解复杂的C/C++声明(陆其明 译) [所有相关帖子]
- 页描述符与物理地址(相关主题帖子总结)
- 如何通过maven官网查询相关依赖的具体代码和版本?
- 加深理解HTTP请求---网络基础TCP/IP
- MySql学习(七) —— 查询性能优化 深入理解MySql如何执行查询
- 正则表达式自已加深理解
- Oracle树查询及相关函数
- oracel 相关查询 Oracle Hint的用法
- Sql Server之旅——第六站 使用winHex利器加深理解数据页
- mysql 查询时间相关
- MySql学习(七) —— 查询性能优化 深入理解MySql如何执行查询
- MySql学习(七) —— 查询性能优化 深入理解MySql如何执行查询
- 理解SQL Server是如何执行查询的 (1/3)
- 理解SQL Server的查询内存授予