开发者

How do I add the results of a MySQL query which splits a column into two to a table?

开发者 https://www.devze.com 2023-04-09 12:33 出处:网络
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:

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), ' '开发者_StackOverflow社区, -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.

I tried adding INSERT conference [Firstname, Lastname] to the start of the Query, but that generated an error. 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
0

精彩评论

暂无评论...
验证码 换一张
取 消