现在我要做得是把 d = c
有个前提条件是b相等,而且a = a + 1
例如:
a b c d
---------------------------------
1 a 20080101
2 a 20080102
3 a 20080103
4 b 20080102
5 c 20080103
结果应该为:
a b c d
---------------------------------
1 a 20080101 20080102
2 a 20080102 20080103
3 a 20080103 null
4 b 20080102 null
5 c 20080103 null
13 个解决方案
#1
你那个a=a+1是什么意思?
#2
select a,b,c, d=(select min(c) from tb where b = t.b and b>t.b) from tb t
#3
update tb
set d=a.c
from tb a
where a.b=tb.b and a.a=tb.a+1
#4
select a,b,c, d=(select min(c) from tb where b = t.b and b>t.b) from tb t
#5
create table tb(a varchar(10) , b varchar(10) , c varchar(10) )
insert into tb values('1' , 'a' , '20080101')
insert into tb values('2' , 'a' , '20080102')
insert into tb values('3' , 'a' , '20080103')
insert into tb values('4' , 'b' , '20080102')
insert into tb values('5' , 'c' , '20080103')
go
select * , d = (select c from tb where b = t.b and a = t.a + 1 ) from tb t
drop table tb
/*
a b c d
---------- ---------- ---------- ----------
1 a 20080101 20080102
2 a 20080102 20080103
3 a 20080103 NULL
4 b 20080102 NULL
5 c 20080103 NULL
(所影响的行数为 5 行)
*/
#6
update a1 set
d=a2.c
from a a1,a a2
where a2.a=a1.a+1
and a2.b=a1.b
#7
!
#8
NND,我2楼写错了,你们也照着抄?
#9
create table tb(a varchar(10) , b varchar(10) , c varchar(10) , d varchar(10))
insert into tb values('1' , 'a' , '20080101',null)
insert into tb values('2' , 'a' , '20080102',null)
insert into tb values('3' , 'a' , '20080103',null)
insert into tb values('4' , 'b' , '20080102',null)
insert into tb values('5' , 'c' , '20080103',null)
go
--查询
select a , b , c, d = (select c from tb where b = t.b and a = t.a + 1 ) from tb t
--更新
update tb set d = (select c from tb where b = t.b and a = t.a + 1 ) from tb t
drop table tb
/*
a b c d
---------- ---------- ---------- ----------
1 a 20080101 20080102
2 a 20080102 20080103
3 a 20080103 NULL
4 b 20080102 NULL
5 c 20080103 NULL
(所影响的行数为 5 行)
*/
#10
我要做得事情是:d 的日期数值是a的值+1对应的a值,再对应的c值,
赋给d
赋给d
#11
更新数据库,而不是只显示出来
#12
强啊
update tb set d = (select c from tb where b = t.b and a = t.a + 1 ) from tb t
加了不连续a相同b后再连续a相同b,测试通过!呵呵
#13
update tb set d = (select c from tb where b = t.b and a = t.a + 1 ) from tb t
很强大
很强大
#1
你那个a=a+1是什么意思?
#2
select a,b,c, d=(select min(c) from tb where b = t.b and b>t.b) from tb t
#3
update tb
set d=a.c
from tb a
where a.b=tb.b and a.a=tb.a+1
#4
select a,b,c, d=(select min(c) from tb where b = t.b and b>t.b) from tb t
#5
create table tb(a varchar(10) , b varchar(10) , c varchar(10) )
insert into tb values('1' , 'a' , '20080101')
insert into tb values('2' , 'a' , '20080102')
insert into tb values('3' , 'a' , '20080103')
insert into tb values('4' , 'b' , '20080102')
insert into tb values('5' , 'c' , '20080103')
go
select * , d = (select c from tb where b = t.b and a = t.a + 1 ) from tb t
drop table tb
/*
a b c d
---------- ---------- ---------- ----------
1 a 20080101 20080102
2 a 20080102 20080103
3 a 20080103 NULL
4 b 20080102 NULL
5 c 20080103 NULL
(所影响的行数为 5 行)
*/
#6
update a1 set
d=a2.c
from a a1,a a2
where a2.a=a1.a+1
and a2.b=a1.b
#7
!
#8
NND,我2楼写错了,你们也照着抄?
#9
create table tb(a varchar(10) , b varchar(10) , c varchar(10) , d varchar(10))
insert into tb values('1' , 'a' , '20080101',null)
insert into tb values('2' , 'a' , '20080102',null)
insert into tb values('3' , 'a' , '20080103',null)
insert into tb values('4' , 'b' , '20080102',null)
insert into tb values('5' , 'c' , '20080103',null)
go
--查询
select a , b , c, d = (select c from tb where b = t.b and a = t.a + 1 ) from tb t
--更新
update tb set d = (select c from tb where b = t.b and a = t.a + 1 ) from tb t
drop table tb
/*
a b c d
---------- ---------- ---------- ----------
1 a 20080101 20080102
2 a 20080102 20080103
3 a 20080103 NULL
4 b 20080102 NULL
5 c 20080103 NULL
(所影响的行数为 5 行)
*/
#10
我要做得事情是:d 的日期数值是a的值+1对应的a值,再对应的c值,
赋给d
赋给d
#11
更新数据库,而不是只显示出来
#12
强啊
update tb set d = (select c from tb where b = t.b and a = t.a + 1 ) from tb t
加了不连续a相同b后再连续a相同b,测试通过!呵呵
#13
update tb set d = (select c from tb where b = t.b and a = t.a + 1 ) from tb t
很强大
很强大