将同一行中的数据复制到另一行 table SQL
Copying data from one row to another in the same table SQL
我很难在同一个 table 中将数据从一行复制到另一行。基本上,我有一种情况,其中列 [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
。我如何归因于 table [form_record]
中属于 Client1 的所有数据(行)并将其复制到 Client2,然后最终删除 Client1 或什至更好,只需简单地替换名称但你们知道得更多。我认为 newdata
可以用作同一个 table form_record
的别名 table 但它似乎不起作用。
我正在使用 Microsoft SQL 2005 Server。
应该这样做,您只需要在 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'";
您的 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'";
我很难在同一个 table 中将数据从一行复制到另一行。基本上,我有一种情况,其中列 [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
。我如何归因于 table [form_record]
中属于 Client1 的所有数据(行)并将其复制到 Client2,然后最终删除 Client1 或什至更好,只需简单地替换名称但你们知道得更多。我认为 newdata
可以用作同一个 table form_record
的别名 table 但它似乎不起作用。
我正在使用 Microsoft SQL 2005 Server。
应该这样做,您只需要在 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'";
您的 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'";