简体   繁体   English

如何在MySQL中将逗号分隔的名称字段分隔为名字,中间名,姓氏和全名?

[英]How to separate a comma separated name field in MySQL in to first name, middle name, last name and fullname?

I have a NAME column and the data is stored like this in a MySql database: 我有一个NAME列,数据像这样存储在MySql数据库中:

Doe,John P 道恩,约翰·P

I would like to break it out into FNAME LNAME MNAME and FULLNAME . 我想将其分为FNAME LNAME MNAMEFULLNAME

If I don't want to tamper with the SQL table, should I create a view? 如果我不想篡改SQL表,是否应该创建视图?

I know how to fix it in PHP with a function but I would rather the processing be done on the database server than the application server. 我知道如何使用函数在PHP中修复它,但是我宁愿在数据库服务器上而不是在应用程序服务器上进行处理。

This is what I have so far. 到目前为止,这就是我所拥有的。 I thinking I need a compound query or something similar. 我想我需要一个复合查询或类似的东西。

SELECT Name
       , SUBSTRING_INDEX(Name,',',-1) As FNAME
       , SUBSTRING_INDEX(Name,',',1)  As LNAME
       , SUBSTRING_INDEX(Name,' ',1)  As MNAME 
  FROM people;

I'm having trouble grabbing the middle name and doing what I think should be CONCAT function to get FULLNAME . 我在获取中间名和执行我认为应该是CONCAT函数来获取FULLNAME遇到麻烦。

It would be great if there was a way (maybe a conditional) to handle names in the database that didn't have middle names. 如果有一种方法(可能是有条件的)来处理数据库中没有中间名的名称,那将是很好的。 (ie DOE,JOHN). (即DOE,JOHN)。

A view is a fine way to get the results you want for future queries. 视图是一种获得所需的未来查询结果的好方法。 The defining query would be: 定义查询将是:

SELECT Name, SUBSTRING_INDEX(SUBSTRING_INDEX(Name, ',', -1), ' ', 1)  As FNAME,
       SUBSTRING_INDEX(Name, ',', 1)  As LNAME,
       SUBSTRING_INDEX(Name, ' ', -1)  As MNAME
FROM people;

This will work assuming that the format is exactly correct as you describe it in the table. 假设该格式与您在表中描述的格式完全正确,那么它将起作用。 Small changes -- such as no middle name or a space after the comma -- would make the syntax more complicated. 较小的更改(例如没有中间名或逗号后有空格)会使语法更加复杂。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 PHP和MySQL:将全名字符串分解为名字,中间名和姓氏并将其存储在变量中 - Php and MySQL: Dissect fullname string into first, middle and last name and stored it in variables 如何将全名列插入姓名和姓氏列 MYSQL - how to insert a fullname column into name and last name columns MYSQL MySQL:从全名字段中提取名字或姓氏 - MySQL: Extracting First Name or Last Name From Full Name Field 如何在mysql中获取逗号分隔的ID名称? - How to get name of comma separated id in mysql? MySQL 如何在处理空时连接中间名、名字和姓氏? - MySQL how to concatenate middle, first, and last name while handling empty? 在 MySQL 数据库中搜索全名或名字或姓氏,名字和姓氏在单独的列中 - Searching full name or first or last name in MySQL database with first and last name in separate columns 如何使用 mySQL 中的代码将“名字姓氏”更改为“姓氏,名字”? - How do I change "First Name Last Name" to "Last Name, First Name" using code in mySQL? MySQL出现问题:CONCAT_WS('',name_first,name_middle,name_last),例如'%keyword%' - Trouble with MySQL: CONCAT_WS(' ', name_first, name_middle, name_last) like '%keyword%' MYSQL 在脏全名上查找名称 - MYSQL find name on dirty fullname 如果单个字段“名称”同时包含名字和姓氏,如何按姓氏排序 - How to get order by last name if single field “name” contains both first name and last name
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM