[英]Splitting data field into two columns & Inserting it to an existing table (MYSQL)
im trying to split a specific column in an already existing table in MYSQL into two columns, AND grab the returning two columns and inserting them into an already existing table. 我试图将MYSQL中已存在的表中的特定列拆分为两列,然后抓取返回的两列并将其插入到已存在的表中。
I just started learning MYSQL couple of days ago, sorry if thats a noob questions: 我几天前才刚刚开始学习MYSQL,如果那是一个菜鸟问题,那么抱歉:
here is what i tried so far: 这是我到目前为止尝试过的:
MYSQL Code begins : 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 Code Ends : MYSQL代码结尾:
this did split my datafield which looks like this '12345-990001' into two columns, one as the ZIP and the second as additional data. 这确实将我的数据字段(看起来像是“ 12345-990001”)分为两列,一列为ZIP,第二列为其他数据。 the problem is that i cant add the return of this into another table that i have. 问题是我不能将此返回的值添加到我拥有的另一个表中。 here is the syntax that im using now (its not working though): 这是即时通讯现在使用的语法(虽然无法使用):
MYSQL Code begins : 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 Code Ends : MYSQL代码结尾:
any help that would be much appreciated, thank you in advance! 任何帮助,将不胜感激,在此先感谢您!
You should always use a column list with insert
. 您应该始终将列列表与insert
。 The problem is that your query is being interpreted as: 问题是您的查询被解释为:
insert into location(select . . .)
And select
is not a valid column. 并且select
不是有效的列。 Also, the word table
is not necessary. 同样,单词table
不是必需的。 So try this: 所以试试这个:
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;
Note: I am guessing what the column names are. 注意:我正在猜测列名是什么。
EDIT: 编辑:
You need to insert a value into all columns that have no default values and cannot accept NULLs. 您需要在没有默认值并且不能接受NULL的所有列中插入一个值。 I would guess that zip_code
is the field you want to populate with zip
: 我猜想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;
Perhaps you want something like this: 也许您想要这样的东西:
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;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.