简体   繁体   English

如何添加将一列分为两张的MySQL查询的结果?

[英]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.

相关问题 我如何在MySQL中查询两个日期,其中datefrom在同一表的第二列中? - How do I query two dates in mysql where the datefrom is in one column and dateto is in the second column of the same table? 如何忽略MySQL查询中JOIN表的结果 - How do I ignore results from JOIN table in MySQL query MySQL如何向已合并来自不同表的两行的搜索查询添加额外的列以显示它来自哪个表 - MySQL how to add an extra column to a search query that has unioned two rows from different tables to show from which table it comes from 如何在 MySQL 查询中引用表点列表示法 - How do I reference table dot column notation in a MySQL query 如何向 MySQL 表添加索引列? - How do I add an index column to a MySQL table? 如何将主键添加到 mysql 表作为第一列 - How do I add a primary key to a mysql table as the first column 如何使用mySQL选择具有一列最大值的结果,按另一列分组? - How do I use mySQL to select results which have a maximum value for one column, grouped by another column? 如何创建第3列,它是查询表时前两列的总和(MySQL) - How do you create a 3rd column which is the sum of the first two column when querying a table (MySQL) mysql 类似查询,如何针对表中的两列进行排序但按一列匹配的结果排序 - mysql like query, how can you sort targeting two columns in a table but sorting by the results matched by one column 如何通过查询在mysql表中添加新列 - How to add a new column to a table in mysql by query
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM