我有一个表,字段为atation_x,atation_x_y,atation_z,atation_id
想用一个语句更新多行的数据,比如:
update 表 set atation_x=1 , atation_y=2 , atation_z=3 where atation_id=1
update 表 set atation_x=2 , atation_y=3 , atation_z=4 where atation_id=2
...
合成一句怎么写呢?还有,如果atation_x的是数值类型的,还用加引号么?

解决方案 »

  1.   

    用UPDATE的from子句:构建一个 A表 存储对应你要修改的数据
    X Y Z ID
    1 2 3 1
    2 3 4 2SQL:
    UPDATE atation表 
    set atation_x=A表.X , atation_y=A表.Y , atation_z=A表.Z 
    FROM atation表 INNER JOIN A表 ON atation表.ID=A表.ID
     
      

  2.   

    update 表 set atation_x=atation_id, atation_y=atation_id+1 , atation_z=atation_id+2  
      

  3.   

    update 表 set atation_x=atation_id , atation_y=atation_id+1 , atation_z=atation_id+2
      

  4.   

    楼上的大哥,atation_x=1 里的1是个变量,不一定值是多少,还需要这么麻烦先构建个表再更新么?那如果这个变量的值变化的话,怎么办呢?
      

  5.   

    --只能这样
    update tb set atation_x=atation_id, atation_y=atation_id+1 , atation_z=atation_id+2
      

  6.   

    只能上面的方法了,
    如果atation_x是数值的话,就不用引号了
      

  7.   

    update 表 
    set atation_x = (case when atation_id = 1 then 1 when atation_id = 2 then 2 else atation_x end),
        atation_y = (case when atation_id = 1 then 2 when atation_id = 2 then 3 else atation_y end), 
        atation_z=  (case when atation_id = 1 then 3 when atation_id = 2 then 4 else atation_z end)
      

  8.   


    合成一句怎么写呢?
    update 表 
    set atation_x = (case when atation_id = 1 then 1 when atation_id = 2 then 2 else atation_x end),
        atation_y = (case when atation_id = 1 then 2 when atation_id = 2 then 3 else atation_y end), 
        atation_z=  (case when atation_id = 1 then 3 when atation_id = 2 then 4 else atation_z end)
    还有,如果atation_x的是数值类型的,还用加引号么? 不需要.楼上的大哥,atation_x=1 里的1是个变量,不一定值是多少,还需要这么麻烦先构建个表再更新么?那如果这个变量的值变化的话,怎么办呢?这个不知道问的什么,能否举例?
      

  9.   

    我的atation_x,atation_y,atation_z保存的都是一个物体的坐标信息,当坐标信息变化的时候,就去数据库更新这个物体的坐标信息,一共是9个物体,所以一共需要更新9行,您上面的貌似行得通,我试试哈。。万分感谢!!!
      

  10.   

    建议你列出你的表结构,并提供测试数据以及基于这些测试数据的所对应正确结果。
       参考一下这个贴子的提问方式http://topic.csdn.net/u/20091130/20/8343ee6a-417c-4c2d-9415-fa46604a00cf.html
       
       1. 你的 create table xxx .. 语句
       2. 你的 insert into xxx ... 语句
       3. 结果是什么样,(并给以简单的算法描述)
       4. 你用的数据库名称和版本(经常有人在MS SQL server版问 MySQL)
       
       这样想帮你的人可以直接搭建和你相同的环境,并在给出方案前进行测试。   
      

  11.   

    貌似ACCESS不能用case when 
    还是感谢高手们了。。散分吧。。