先看看数据结构
create table a
( ID CHAR(16),
  NAME CHAR(16))
CREATE TABLE B
( ID CHAR(16),
  NAME CHAR(16),
  id_a char(16)
)
假如B中有记录
id   name     id_a
__________________
0    null     1
1    null     2
假如A中有记录
id    name 
------------
1    AAAAA
2    BBBBBB
下列的语句就是:将a.id=b.id_a相等的记录,将name对应的填入B中,结果为id   name     id_a
__________________
0    AAAAA     1
1   BBBBBB     2大家研究一下,
1.  update b set b.name=(select name from a as p where p.id=b.id_a) where id_a in (select id from a)  
2.  update b set b.name=(select name from a as p where p.id=b.id_a)
3.  update b set b.name=(select name from a as p,b where p.id=b.id_a)上面1和2正确,,但是我不清楚,,为什么会达到一样的效果,
而3是错误的.原因也就多了个B,,,我也搞不清楚.??
请大家进来热烈讨论.

解决方案 »

  1.   

    1,2怎么可能一样,你把a中去掉一条数据看看。
    你的结果一样是因为a,b表id是全匹配的,当然没差别了。3 select name from a as p,b where p.id=b.id_a
    这是个内连语句,a,b都有name列,在select列表中你未指定name属哪个列,所以会报错这些都是按语义直写的语句,效率很差。update x set name=a.name from b x inner join a on a.id=x.id_a
      

  2.   

    update b set name=a.name from a where  exists(select 1 from a where a.id=b.id_a)
    不知道用join会不会快些呢?
      

  3.   

    update b set name=a.name from a where  exists(select 1 from a where a.id=b.id_a)
    这里的WHERE部分怎么理解 ?
      

  4.   

    update b set name=a.name from a where  exists(select 1 from a where a.id=b.id_a)
    这里的WHERE部分怎么理解 ?
      

  5.   

    创意、自由、灵活,超强的报表功能,
    独特的双数据源连接,全功能的表格组件!http://www.anylib.com
      

  6.   

    update b set name=a.name from a where  exists(select 1 from a where a.id=b.id_a)
    这里的WHERE部分怎么理解 ?
      

  7.   

    有这个必要吗?
    id   name     id_a
    __________________
    0    null     1
    1    null     2
    假如A中有记录
    id    name 
    ------------
    1    AAAAA
    2    BBBBBB
    update b
    set name = a.name
    from a,b
    where a.id = b.id_a