我试图将MYSQL中已存在的表中的特定列拆分为两列,然后获取返回的两列并将它们插入到已存在的表中。
我刚刚开始学习MYSQL,对不起,如果那是一个菜鸟问题:
这是我到目前为止所尝试的内容:
MYSQL代码开始:
SELECT substring_index(substring_index(ZIP_code, '-',1), '-', -1) as ZIP,
substring_index(substring_index(ZIP_code, '-',2), '-', -1) as Extended_info,
from Region.Address
MYSQL代码结束:
这确实将我的数据字段(看起来像这个'12345-990001)分成两列,一列作为ZIP,第二列作为附加数据。问题是我无法将此返回到我拥有的另一个表中。这是我现在使用的语法(虽然它不起作用):
MYSQL代码开始:
INSERT INTO TABLE Location
(SELECT substring_index(substring_index(ZIP_code, '-',1), '-', -1) as ZIP,
substring_index(substring_index(ZIP_code, '-',2), '-', -1) as Extended_info,
from Region.Address) where ZIP is varchar(64), AND Extended_info is varchar(64)
MYSQL代码结束:
任何非常感谢的帮助,请提前感谢!
答案 0 :(得分:0)
您始终使用insert
的列列表。问题是您的查询被解释为:
insert into location(select . . .)
并且select
不是有效列。此外,单词table
不是必需的。所以试试这个:
INSERT INTO Location(zip, extended_info)
SELECT substring_index(substring_index(ZIP_code, '-',1), '-', -1) as ZIP,
substring_index(substring_index(ZIP_code, '-',2), '-', -1) as Extended_info,
from Region.Address;
注意:我猜测列名是什么。
编辑:
您需要在没有默认值的所有列中插入值,并且不能接受NULL。我猜想zip_code
是您希望填充zip
的字段:
INSERT INTO Location(zip_code, extended_info)
SELECT substring_index(substring_index(ZIP_code, '-',1), '-', -1) as ZIP,
substring_index(substring_index(ZIP_code, '-',2), '-', -1) as Extended_info,
from Region.Address;
也许你想要这样的东西:
INSERT INTO Location(zip_code, zip, extended_info)
SELECT '' as zip_code,
substring_index(substring_index(ZIP_code, '-',1), '-', -1) as ZIP,
substring_index(substring_index(ZIP_code, '-',2), '-', -1) as Extended_info,
from Region.Address;