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

本文演示了如何使用SQL创建表、插入数据、更新字段、删除列及最终显示表内容等操作。通过具体的SQL语句展示了表结构的修改过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值