[英]How do I add the results of a MySQL query which splits a column into two to a table?
I am running the following query on a table, which splits the first name and last name of each person in the Name column of the table into Firstname and Lastname: 我在一个表上运行以下查询,该查询将表的“名称”列中每个人的名字和姓氏分为“名字”和“姓氏”:
SELECT SUBSTRING_INDEX(SUBSTRING_INDEX(Name, ' ', 1), ' ', -1) as Firstname,
SUBSTRING_INDEX(SUBSTRING_INDEX(Name, ' ', 2), ' ', -1) as Lastname
FROM conference;
This works fine. 这很好。 I would now like to add the results of this to two new columns in the table which I have called Firstname and Lastname. 现在,我想将其结果添加到表中名为“ Firstname”和“ Lastname”的两个新列中。
I tried adding INSERT conference [Firstname, Lastname]
to the start of the Query, but that generated an error. 我尝试将INSERT conference [Firstname, Lastname]
到查询的开头,但是生成了错误。 Could someone help with the correct way of doing this? 有人可以提供正确的方法帮助吗?
Thanks, 谢谢,
Nick 缺口
If your intent is to update existing rows with those new fields instead of inserting new records, this should work 如果您打算使用这些新字段更新现有行,而不是插入新记录,则应该可以
UPDATE Conference
SET
Firstname = SUBSTRING_INDEX(SUBSTRING_INDEX(Name, ' ', 1), ' ', -1),
Lastname = SUBSTRING_INDEX(SUBSTRING_INDEX(Name, ' ', 2), ' ', -1)
have you tried: 你有没有尝试过:
select SUBSTRING_INDEX(SUBSTRING_INDEX(Name, ' ', 1), ' ', -1) as Firstname,
SUBSTRING_INDEX(SUBSTRING_INDEX(Name, ' ', 2), ' ', -1) as Surname
into conference
from conference
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.