表TB
userid  funid p1 p2 p3 p4 p5 p6 p7 otpower
admin   100   1  1  1  1  0  1  0    1
admin   101   1  0  1  1  1  1  1    1
guest   100   0  0  0  0  0  0  0    0
guest   101   0  0  0  0  0  0  0    0
如何更新guest的内容,使得userid='admin' 和userid='guest'后面的都一样

解决方案 »

  1.   

    update t1 
    set t1.p1=t2.p1,
    t1.p2=t2.p2,
    t1.p3=t2.p3,
    t1.p4=t2.p4,
    t1.p5=t2.p5,
    t1.p6=t2.p6,
    t1.p7=t2.p7,
    t1.otpower=t2.ptpower
    from tb t1,tb t2
    where t1.userid='guest' and t2.userid='admin' and t1.funid=t2.funid
      

  2.   

    表TB
    userid funid p1 p2 p3 p4 p5 p6 p7 otpower
    admin 100 1 1 1 1 0 1 0 1
    admin 101 1 0 1 1 1 1 1 1
    guest 100 0 0 0 0 0 0 0 0
    guest 101 0 0 0 0 0 0 0 0
    如何更新guest的内容,使得userid='admin' 和userid='guest'后面的都一样
    userid funid p1 p2 p3 p4 p5 p6 p7 otpower
    admin 100 1 1 1 1 0 1 0 1
    admin 101 1 0 1 1 1 1 1 1
    guest 100 1 1 1 1 0 1 0 1
    guest 101 1 0 1 1 1 1 1 1
      

  3.   

    --> 测试数据:[ta]
    if object_id('[ta]') is not null drop table [ta]
    go
    create table [ta]([userid] varchar(5),[funid] int,[p1] int,[p2] int,[p3] int,[p4] int,[p5] int,[p6] int,[p7] int,[otpower] int)
    insert [ta]
    select 'admin',100,1,1,1,1,0,1,0,1 union all
    select 'admin',101,1,0,1,1,1,1,1,1 union all
    select 'guest',100,0,0,0,0,0,0,0,0 union all
    select 'guest',101,0,0,0,0,0,0,0,0--------------------------------查询开始------------------------------
    update a  set p1=b.p1,p2=b.p2,p3=b.p3, p4=b.p4,p5=b.p5, p6=b.p6,p7=b.p7,otpower=b.otpower 
    from 
    (select * from [ta] where userid='guest') a,
    (select * from [ta] where userid='admin') b
    where a.funid=b.funid
    select * from [ta]
    /*
    userid funid       p1          p2          p3          p4          p5          p6          p7          otpower
    ------ ----------- ----------- ----------- ----------- ----------- ----------- ----------- ----------- -----------
    admin  100         1           1           1           1           0           1           0           1
    admin  101         1           0           1           1           1           1           1           1
    guest  100         1           1           1           1           0           1           0           1
    guest  101         1           0           1           1           1           1           1           1(4 行受影响)
    */
      

  4.   

    --or
    update a  set p1=b.p1,p2=b.p2,p3=b.p3, p4=b.p4,p5=b.p5, p6=b.p6,p7=b.p7,otpower=b.otpower 
    from [ta] a,[ta] b
    where a.userid='guest' and b.userid='admin' and a.funid=b.funid
    ps 这两个语句效率是一样的
      

  5.   

    ----------------------------------------------------------------------------------
    -- Author : htl258(Tony)
    -- Date   : 2010-05-15 09:02:38
    -- Version: Microsoft SQL Server 2008 (RTM) - 10.0.1600.22 (Intel X86) 
    --          Jul  9 2008 14:43:34 
    --          Copyright (c) 1988-2008 Microsoft Corporation
    --          Developer Edition on Windows NT 5.1 <X86> (Build 2600: Service Pack 2)
    -- Blog   : http://blog.csdn.net/htl258
    ------------------------------------------------------------------------------------> 生成测试数据表: [TB]
    IF OBJECT_ID('[TB]') IS NOT NULL
    DROP TABLE [TB]
    GO
    CREATE TABLE [TB] ([userid] [nvarchar](10),[funid] [int],[p1] [int],[p2] [int],[p3] [int],[p4] [int],[p5] [int],[p6] [int],[p7] [int],[otpower] [int])
    INSERT INTO [TB]
    SELECT 'admin','100','1','1','1','1','0','1','0','1' UNION ALL
    SELECT 'admin','101','1','0','1','1','1','1','1','1' UNION ALL
    SELECT 'guest','100','0','0','0','0','0','0','0','0' UNION ALL
    SELECT 'guest','101','0','0','0','0','0','0','0','0'--SELECT * FROM [TB]-->SQL查询如下:
    UPDATE A SET 
    A.P1=B.P1,A.P2=B.P2,A.P3=B.P3,A.P4=B.P4,A.P5=B.P5,A.P6=B.P6,A.P7=B.P7,A.OTPOWER=B.OTPOWER
    FROM TB A
    JOIN TB B
    ON B.USERID='ADMIN' AND A.USERID='GUEST'
    AND A.FUNID=B.FUNIDSELECT * FROM TB
    /*
    userid     funid       p1          p2          p3          p4          p5          p6          p7          otpower
    ---------- ----------- ----------- ----------- ----------- ----------- ----------- ----------- ----------- -----------
    admin      100         1           1           1           1           0           1           0           1
    admin      101         1           0           1           1           1           1           1           1
    guest      100         1           1           1           1           0           1           0           1
    guest      101         1           0           1           1           1           1           1           1(4 行受影响)
    */