一个表A:id    hy   leftn  rightn  
1    101     0      102
2    102     103     0
3    103     113     112查询leftn,匹配hy,如果hy中不存在 那么把hy中不存在的lefttn归零如上图,应该把1、3归0请问如何写改语句?

解决方案 »

  1.   

    create table a(id int,hy int ,leftn int,rightn int)
    insert into a
    select 1,101,0,102 union all
    select 2,102,103,0 union all
    select 3,103,113,112 update A
    set leftn=case when exists(select 1 from A T where T.hy=A.leftn) then leftn else 0 endselect * from a 
    /*
    id          hy          leftn       rightn      
    ----------- ----------- ----------- ----------- 
    1           101         0           102
    2           102         103         0
    3           103         0           112
    */
    drop table a
      

  2.   

    kevinsum() ( ) 信誉:100    Blog   加为好友  2007-06-08 10:46:42  得分: 0  
     
     
       表中有6万多条数据哦~怎么办?
      
     
    -----------------------------------
    有關係嗎?
      

  3.   

    select 1,101,0,102 union all
    select 2,102,103,0 union all
    select 3,103,113,112 这个怎么回事呢?
    不是很明白
      

  4.   

    select 1,101,0,102 union all
    select 2,102,103,0 union all
    select 3,103,113,112 这个怎么回事呢?
    不是很明白
    ---------------------------不是吧.不知说什么好啊
      

  5.   

    那你就執行下面這條語句吧
    update A
    set leftn=case when exists(select 1 from A T where T.hy=A.leftn) then leftn else 0 end
      

  6.   

    还想请问一句,select 1 from A  为什么是select 1?
      

  7.   

    update hyclub
    set rightnumber=case when exists(select 1 from hyclub T where T.hynumber=hyclub.rightnumber) then rightnumber else 0 end出现一下错误:将 nvarchar 值 'wh888' 转换为数据类型为 int 的列时发生语法错误。
      

  8.   

    用这个!!update hyclub
    set rightnumber=case when exists(select 1 from hyclub T where ltrim(T.hynumber)=ltrim(hyclub.rightnumber)) then rightnumber else 0 end
      

  9.   

    To  kevinsum()  
    insert into a select 1,101,0,102 union all
    select 2,102,103,0 union all
    select 3,103,113,112 
    这是插入记录的语句相当于insert into a values(1,101,0,102)
    insert into a values(2,10,103,0)