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
精彩评论