0

我有一个从 xml 阅读器填充的临时表,然后替换为我的站点的最终生产表。现在列匹配,但我想在生产表中添加一个列,该列将被手动更新,并且在我们从临时表更新时不会被覆盖。

示例:临时表有列:

Name
Address
Phone

生产表有:

Name 
Address 
Phone
Date

目前我使用:

REPLACE INTO Tdata select * from temp_Tdata;

如何在不更改当前日期列的情况下从临时表中提取数据。这些表有超过 25 列,所以如果可以的话,我宁愿不必列出所有列名。

谢谢你的帮助。

更新

REPLACE INTO Tdata (Name, Address, Phone) select (Name, Address, Phone) from temp_Tdata;

不起作用,因为 Date 列被重置为默认值,丢失了对其进行的任何手动更改。

4

3 回答 3

1

更新解决方法REPLACE

REPLACE INTO Tdata (Name, Address, Phone, Date)
SELECT tt.Name, tt.Address, tt.Phone, t.Date
  FROM temp_Tdata tt LEFT JOIN 
       Tdata t ON tt.Name = t.Name;

sqlfiddle

Now you can create a view that brings Date column to temp data

CREATE VIEW vw_temp_Tdata
AS
SELECT tt.Name, tt.Address, tt.Phone, t.Date
  FROM temp_Tdata tt LEFT JOIN 
       Tdata t ON tt.Name = t.Name;

and then use it like this

REPLACE INTO Tdata 
 SELECT * FROM vw_temp_Tdata;

sqlfiddle

Original answer was deleted since it won't work with REPLACE.

于 2013-03-06T21:26:49.153 回答
1
UPDATE Tdata
SET Name = temp_TData.Name,
Address = temp_Tdata.Address,
Phone = temp_TData.Phone
WHERE Tdata.Name = temp_Tdata.Name
于 2013-03-06T21:28:26.187 回答
0

If I understand correctly, you need to:

  1. Add a column to your destination table
  2. Insert the data from your original table (which has more columns than the destination table had) into your destination table

If that's true, then you need to alter the destination table before updating it:

ALTER TABLE Tdata
    ADD COLUMN newColumn Varchar(50); -- Just an example

Then you can update (or insert) the data.

于 2013-03-06T21:29:20.677 回答