doubi6669 2017-06-20 18:20
浏览 412
已采纳

在同一个表SQL中将数据从一行复制到另一行

I'm having a hard time copying data from one row to another in the same table. Basically, I have a case where the column [I/O#] equals 'Client1' but I would like to make the [I/O#] column equal to 'Client2' without having the data attributed to Client1 erased, i.e. I want to keep the data associated with Client1 but just replace the name to Client2.

I attempted the following sql query in my php file but in vain :

$sql_UPDATE_query ="UPDATE [$connectionInfo[Database]].[dbo].[form_record]
                    SET
                        [Last_Edit] = newdata.[Last_Edit],
                        [User_Name] = newdata.[User_Name],
                        [Computer_Name] = newdata.[Computer_Name],
                        [form_name] = newdata.[form_name],
                        [Date] = newdata.[Date],
                        [facility] = newdata.[facility],
                        [Count] = newdata.[Count],
                        [Start_Time] = newdata.[Start_Time],
                        [Stop_Time] = newdata.[Stop_Time],
                        [MW/Hrs_Start] = newdata.[MW/Hrs_Start],
                        [MW/Hrs_Stop] = newdata.[MW/Hrs_Stop],
                        [M³] = newdata.[M³],
                        [MCF] = newdata.[MCF],
                        [Litres] = newdata.[Litres],
                        [Description] = newdata.[Description],
                        [Lock] = newdata.[Lock]
                    FROM
                        (
                        SELECT
                            [Last_Edit],
                            [User_Name],
                            [Computer_Name],
                            [form_name],
                            [Date],
                            [facility],
                            [Count],
                            [Start_Time],
                            [Stop_Time],
                            [MW/Hrs_Start],
                            [MW/Hrs_Stop],
                            [M³],
                            [MCF],
                            [Litres],
                            [Description],
                            [Lock]
                        FROM [$connectionInfo[Database]].[dbo].[form_record]
                        WHERE
                            [I/O#] = '$PrevIO'
                        ) newdata
                    WHERE
                        [I/O#] = '$IO'";

By the way $PrevIO and $IO are already defined in my php file (just not showing it) so that's not where the error lies.

Now, let's say $PrevIO='Client1' and $IO='Client2. How can I attribute all the data (row) in table [form_record] that belongs to Client1 and copy it to Client2 and then eventually delete Client1 or even better, simply replace simply the name but you guys know better. I thought that newdata could be used as an alias table for the same table form_record but it seems like it doesn't work.

I'm using Microsoft SQL 2005 Server.

  • 写回答

2条回答 默认 最新

  • duandao2083 2017-06-20 18:26
    关注

    That should do it, you only need to include the proper join in the table.

    $sql_UPDATE_query ="UPDATE UPD
                        SET
                            [Last_Edit] = newdata.[Last_Edit],
                            [User_Name] = newdata.[User_Name],
                            [Computer_Name] = newdata.[Computer_Name],
                            [form_name] = newdata.[form_name],
                            [Date] = newdata.[Date],
                            [facility] = newdata.[facility],
                            [Count] = newdata.[Count],
                            [Start_Time] = newdata.[Start_Time],
                            [Stop_Time] = newdata.[Stop_Time],
                            [MW/Hrs_Start] = newdata.[MW/Hrs_Start],
                            [MW/Hrs_Stop] = newdata.[MW/Hrs_Stop],
                            [M³] = newdata.[M³],
                            [MCF] = newdata.[MCF],
                            [Litres] = newdata.[Litres],
                            [Description] = newdata.[Description],
                            [Lock] = newdata.[Lock]
                        FROM [$connectionInfo[Database]].[dbo].[form_record] UPD
                        INNER JOIN 
                            (
                            SELECT
                                [Last_Edit],
                                [User_Name],
                                [Computer_Name],
                                [form_name],
                                [Date],
                                [facility],
                                [Count],
                                [Start_Time],
                                [Stop_Time],
                                [MW/Hrs_Start],
                                [MW/Hrs_Stop],
                                [M³],
                                [MCF],
                                [Litres],
                                [Description],
                                [Lock]
                            FROM [$connectionInfo[Database]].[dbo].[form_record]
                            WHERE
                                [I/O#] = '$PrevIO'
                            ) newdata ON ***newdata.field = UPD.field***
                        WHERE
                            [I/O#] = '$IO'";
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法
  • ¥15 可否在不同线程中调用封装数据库操作的类
  • ¥15 微带串馈天线阵列每个阵元宽度计算
  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据