简体   繁体   English

在SQL中执行语句后,所有数据都为NULL

[英]All data NULL after executing statement in SQL

Statement I used: 我使用的声明:

INSERT INTO table (addr2, addr3) 
    SELECT City, State 
    FROM table

I tried to reverse, but I don't know how. 我试图扭转,但我不知道如何。

I want to get my data back. 我想要恢复我的数据。 All columns and fields are now null . 所有列和字段现在都为null

You said: 你说:

... basically just copying all the data from city and state and placing the data into the addr2 and addr3.fields ...基本上只是复制来自城市和州的所有数据,并将数据放入addr2和addr3.fields

If you want to replace all existing addr2 with city and addr3 with state in all rows of the table, you want UPDATE , not INSERT : 如果要在表的所有行addr3所有现有addr2替换为cityaddr3state ,则需要UPDATE ,而不是INSERT

UPDATE foo SET addr2 = city, addr3 = state;

... where foo is the name of the table. ...其中foo是表的名称。

Note, that will replace any values you previously had for addr2 and addr3 , so use this carefully. 请注意,这将替换您之前对addr2addr3任何值,因此请谨慎使用。 If you only want to replace addr2 and addr3 where they are NULL , you can do: 如果您只想将addr2addr3替换为NULL ,则可以执行以下操作:

UPDATE foo SET addr2 = city WHERE addr2 IS NULL;
UPDATE foo SET addr3 = state WHERE addr3 IS NULL;

Or, perhaps: 也许:

UPDATE foo SET addr2 = city, addr3 = state WHERE addr2 IS NULL AND addr3 IS NULL;
UPDATE foo SET addr3 = CONCAT(city, ', ', state) WHERE addr2 IS NOT NULL AND addr3 IS NULL;

Or, depending upon what SQL engine you're using, you can also use ISNULL() : 或者,根据您使用的SQL引擎,您还可以使用ISNULL()

UPDATE foo SET addr2 = ISNULL(addr2, city), addr3 = ISNULL(addr3, state);

So, delete the rows that you just inserted and then UPDATE the remaining rows as appropriate. 因此,删除刚刚插入的行,然后根据需要UPDATE剩余的行。 You may want to backup your data first, just to be safe. 您可能希望先备份数据,以确保安全。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM