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

时间:2017-06-20 18:20:49

标签: php sql sql-server sql-server-2005

我很难在同一个表格中将数据从一行复制到另一行。基本上,我有一个列[I/O#]等于'Client1'的情况,但我希望[I/O#]列等于'Client2',而不会将归属于Client1的数据删除,即我希望保留与Client1相关联的数据,但只需将名称替换为Client2

我在我的php文件中尝试了以下sql查询但是徒劳无功:

$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'";

顺便说一句$PrevIO$IO已经在我的php文件中定义了(只是没有显示),所以这不是错误的所在。

现在,让我们说$PrevIO='Client1'$IO='Client2。如何归属于属于Client1的表[form_record]中的所有数据(行)并将其复制到Client2,然后最终删除Client1甚至更好,只需简单地替换名称,但你们知道的更好。我认为newdata可以用作同一个表form_record的别名表,但似乎它不起作用。

我正在使用Microsoft SQL 2005 Server。

2 个答案:

答案 0 :(得分:1)

应该这样做,你只需要在表中包含正确的连接。

$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 :(得分:0)

您的join condition遗失了:

$sql_UPDATE_query ="UPDATE A
                    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] A
                    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 = A.field
                    WHERE
                        [I/O#] = '$IO'";