一个高难度的SQL语句
表1:test1
id name birthid cardid
1 aaa 123
2 bbb 456
3 ccc 789
4 ddd 012 012345
表2:test2
birthid cardid
123 123456
456 456789
789 567890
我怎么通过SQL语句,把test1中的cardid1值改成test1的birthid对应的值,执行结果应该是:
表1:test1
id name birthid cardid
1 aaa 123 123456
2 bbb 456 456789
3 ccc 789 567890
4 ddd 012 012345
多谢了,各位高手!
问题点数:20、回复次数:15Top
1 楼shenzhenzsf(郑帅锋)回复于 2006-03-15 22:12:21 得分 0
不好意思,可能我的表达不太清楚,但是看我的表,以及结果能理解我的意思,多谢了,各位!Top
2 楼wangtiecheng(不知不为过,不学就是错!)回复于 2006-03-15 22:13:05 得分 2
update test1 set cardid = b.cardid
from test1 a inner join test2 b on a.birthid = b.birthidTop
3 楼huailairen(流浪猫--很想养只猫,带着它到处流浪。)回复于 2006-03-15 22:27:32 得分 0
create table test1(id int , name varchar(5), birthid varchar(5), cardid varchar(10))
insert into test1 values(1 , 'aaa', '123',null)
insert into test1 values(2 ,'bbb' , '456' ,null)
insert into test1 values(3 ,'ccc' , '789',null)
insert into test1 values(4 , 'ddd' , '012' , '012345')
create table test2 (birthid varchar(5), cardid varchar(10))
insert into test2 values('123','123456')
insert into test2 values('456','456789')
insert into test2 values('789','567890')
select * from test2
select *
from test1 a
where a.cardid is not null
union
select a.id,a.name,a.birthid, b.cardid
from test1 a,test2 b
where a.birthid=b.birthid and a.cardid is null
Top
4 楼huailairen(流浪猫--很想养只猫,带着它到处流浪。)回复于 2006-03-15 22:31:58 得分 0
理解错了
可以用
update test1 set cardid=(select cardid from test2 where test1.birthid=test2.birthid )
where cardid is null or cardid =''
Top
5 楼zjdyzwx(十一月猪)回复于 2006-03-16 08:35:57 得分 1
DECLARE @T1 TABLE(ID INT,NAME VARCHAR(8),BIRTHID INT,CARDID VARCHAR(8))
DECLARE @T2 TABLE(BIRTHID INT , CARDID VARCHAR(8))
INSERT INTO @T1
SELECT 1, 'aaa', 123, '' UNION ALL
SELECT 2, 'bbb', 456, '' UNION ALL
SELECT 3, 'ccc', 789, '' UNION ALL
SELECT 4, 'ddd', 012, '012345'
INSERT INTO @T2
SELECT 123, '123456' UNION ALL
SELECT 456, '456789' UNION ALL
SELECT 789, '567890'
UPDATE @T1 SET CARDID = B.CARDID
FROM @T1 A,@T2 B
WHERE A.BIRTHID = B.BIRTHID
SELECT * FROM @T1
WHERE 1 = 1Top
6 楼fiele(半导体)回复于 2006-03-16 09:42:53 得分 0
不是吧, 这也叫超难Top
7 楼xianggang101(tanjun)回复于 2006-03-16 11:10:47 得分 2
t1:表1
t2:表2
update t1 set cardid=(select cardid from (select t1.id,t2.cardid from t2,t1 where t1.birthid=t2.birthid) a where a.id=t1.id)Top
8 楼xianggang101(tanjun)回复于 2006-03-16 11:11:07 得分 0
简单得不能够再简单了。Top
9 楼jianjian54(键键)回复于 2006-03-16 11:29:39 得分 0
create table test1(id int , name varchar(5), birthid varchar(5), cardid varchar(10))
insert into test1 values(1 , 'aaa', '123',null)
insert into test1 values(2 ,'bbb' , '456' ,null)
insert into test1 values(3 ,'ccc' , '789',null)
insert into test1 values(4 , 'ddd' , '012' , '012345')
create table test2 (birthid varchar(5), cardid varchar(10))
insert into test2 values('123','123456')
insert into test2 values('456','456789')
insert into test2 values('789','567890')
--
-- select * from test2
--
--
-- select *
-- from test1 a
-- where a.cardid is not null
-- union
-- select a.id,a.name,a.birthid, b.cardid
-- from test1 a,test2 b
-- where a.birthid=b.birthid and a.cardid is null
update test1 set cardid=b.cardid
from test1 a join test2 b on a.birthid=b.birthidTop
10 楼tianyan316(与狼共舞,舞者岂是羊)回复于 2006-03-16 11:44:49 得分 10
直接用UPDATE就可以了`:
update test1 set test1.cardid=test2.cardid from test1,test2 where test1.birthid=test2.birthidTop
11 楼tntzbzc(华裔大魔王—抗日要从娃娃抓起)回复于 2006-03-16 13:35:41 得分 0
超难???
.............................
这不是欺骗群广大众们的感情嘛Top
12 楼aniude(重返荣耀)回复于 2006-03-16 14:29:42 得分 2
create table test1(id int , name varchar(5), birthid varchar(5), cardid varchar(10))
insert into test1 values(1 , 'aaa', '123',null)
insert into test1 values(2 ,'bbb' , '456' ,null)
insert into test1 values(3 ,'ccc' , '789',null)
insert into test1 values(4 , 'ddd' , '012' , '012345')
create table test2 (birthid varchar(5), cardid varchar(10))
insert into test2 values('123','123456')
insert into test2 values('456','456789')
insert into test2 values('789','567890')
update test1
set
cardid=(select test2.cardid from test2 where test1.birthid=test2.birthid)
where cardid is null
select * from test1Top
13 楼aniude(重返荣耀)回复于 2006-03-16 14:29:59 得分 0
id name birthid cardid
----------- ----- ------- ----------
1 aaa 123 123456
2 bbb 456 456789
3 ccc 789 567890
4 ddd 012 012345Top
14 楼chenyu112(晨雨)回复于 2006-03-16 15:40:12 得分 0
同意wangtiecheng(cappuccino)Top
15 楼wakinhui(秋风浪萍)回复于 2006-03-16 15:59:48 得分 3
update test1
set cardid = (select cardid from test2 t2 where t2.birthid = t1.birthid )
from test1 t1 where t1.cardid is null or t1.cardid = ''
update test1 set t1.cardid = t2.cardid
from test1 t1, test2 t2
where t1.birthid = t2.birthid and t1.cardid is null or t1.cardid = ''Top




