create table test 
(
 cid int identity(1,1),
 code varchar(20) ,
 cname varchar(20) ,
 country int 
 primary key(cid,code)
)
insert into test 
select 'CN','中国',1 union all
select 'US','美国',2 union all
select 'PEK','北京',1 union all
select 'SIA','西安',1 create table test2
(
  id int primary key identity(1,1),
  code varchar(20),
  country int
)
insert into test2 
select 'CN',3  union all
select 'US',4
update test set country=t2.country
from test t,test2 t2
where t.code=t2.code结果为:
1 CN 中国 3
2 US 美国 4
3 PEK 北京 1
4 SIA 西安 1
要求为:
1 CN 中国 3
2 US 美国 4
3 PEK 北京 3
4 SIA 西安 3

解决方案 »

  1.   

    insert into test2 
    select 'CN',3  union all
    select 'US',4你只插入两行呀,PEK和SIA对应的code没有呀
      

  2.   

    create table test 
    (
     cid int identity(1,1),
     code varchar(20) ,
     cname varchar(20) ,
     country int 
     primary key(cid,code)
    )
    insert into test 
    select 'CN','中国',1 union all
    select 'US','美国',2 union all
    select 'PEK','北京',1 union all
    select 'SIA','西安',1 create table test2
    (
      id int primary key identity(1,1),
      code varchar(20),
      country int
    )
    insert into test2 
    select 'CN',3  union all
    select 'US',4
    /*
    要求为:
    1    CN    中国    3
    2    US    美国    4
    3    PEK    北京    3
    4    SIA    西安    3
    */
    select t1.cid,t1.code,t1.cname,t2.country from test t1 join test2 t2
    on t1.country=t2.id
      

  3.   

    create table test 
    (
     cid int identity(1,1),
     code varchar(20) ,
     cname varchar(20) ,
     country int 
     primary key(cid,code)
    )
    insert into test 
    select 'CN','中国',1 union all
    select 'US','美国',2 union all
    select 'PEK','北京',1 union all
    select 'SIA','西安',1 create table test2
    (
      id int primary key identity(1,1),
      code varchar(20),
      country int
    )
    insert into test2 
    select 'CN',3  union all
    select 'US',4select t1.cid,t1.code,t1.cname,t2.country from test t1 join test2 t2
    on t1.country=t2.idcid         code                 cname                country
    ----------- -------------------- -------------------- -----------
    1           CN                   中国                   3
    2           US                   美国                   4
    3           PEK                  北京                   3
    4           SIA                  西安                   3
      

  4.   

    create table test 
    (
     cid int identity(1,1),
     code varchar(20) ,
     cname varchar(20) ,
     country int 
     primary key(cid,code)
    )
    insert into test 
    select 'CN','中国',1 union all
    select 'US','美国',2 union all
    select 'PEK','北京',1 union all
    select 'SIA','西安',1 create table test2
    (
      id int primary key identity(1,1),
      code varchar(20),
      country int
    )
    insert into test2 
    select 'CN',3  union all
    select 'US',4
    update
      test set country=t2.country
    from
      test t,test2 t2
    where 
      t.country=t2.id
    select * from testdrop table test ,test2 
    /*cid         code                 cname                country
    ----------- -------------------- -------------------- -----------
    1           CN                   中国                   3
    2           US                   美国                   4
    3           PEK                  北京                   3
    4           SIA                  西安                   3(4 行受影响)
    */
      

  5.   

    第一个表为国家城市综合表
     
    第二个为国家表我要的是拿第二各表的country字段更新第一个表
    第二表的顺序是混乱的所以5楼的根据ID更新在实际数据中是办不到的
      

  6.   


    update test 
    set country = (select max(d.country) from 
    (select a.country,a.code from test a join test2 b on a.code = b.code) c
    join test2 d on c.code =d.code and test.country = c.country
    )
      

  7.   

    实际上应该说明
    select 'PEK','北京',1 union all
    select 'SIA','西安',1 属于哪一个级别,即后面的1标示的应该是父节点
      

  8.   


    if object_id('tempdb..#test') is not null
    drop table #test
    if object_id('tempdb..#test2') is not null
    drop table #test2
    go
    create table #test 
    (
     cid int identity(1,1),
     code varchar(20) ,
     cname varchar(20) ,
     country int 
     primary key(cid,code)
    )
    insert into #test 
    select 'CN','中国',1 union all
    select 'US','美国',2 union all
    select 'PEK','北京',1 union all
    select 'SIA','西安',1 create table #test2
    (
      id int primary key identity(1,1),
      code varchar(20),
      country int
    )
    insert into #test2 
    select 'CN',3  union all
    select 'US',4select c.cid,a.code,a.cname,b.country 
    from #test a
    inner join #test2 b  on a.code = b.code
    inner join #test c on a.country = c.countrycid         code                 cname                country     
    ----------- -------------------- -------------------- ----------- 
    1           CN                   中国                   3
    2           US                   美国                   4
    3           CN                   中国                   3
    4           CN                   中国                   3
      

  9.   


    create table test 
    (
     cid int identity(1,1),
     code varchar(20) ,
     cname varchar(20) ,
     country int 
     primary key(cid,code)
    )
    insert into test 
    select 'CN','中国',1 union all
    select 'US','美国',2 union all
    select 'BOS','波士顿',2 union all
    select 'PEK','北京',1 union all
    select 'SIA','西安',1 create table test2
    (
      id int primary key identity(1,1),
      code varchar(20),
      country int
    )
    insert into test2 
    select 'CN',4  union all
    select 'US',3
    update test set country=t2.country
    from test t,test2 t2
    where t.code=t2.code结果为:
               
    1    CN    中国    4
    2    US    美国    3
    3    PEK    北京    1
    4    SIA    西安    1
    5    BOS   波士顿   2
    要求为:
    1    CN    中国    4
    2    US    美国    3
    3    PEK    北京    4
    4    SIA    西安    4
    5    BOS    波士顿   3北京为中国下的城市更新前country为1更新后对应为4
    一表数据量很大