简体   繁体   English

将全名分成名字和姓氏 MySQL

[英]seperate full name into first name and last name MySQL

I started my first year of learnig software development and there is a question I just can't solve, and sadly because of the corona virus I can't ask a teacher util next week.我开始学习软件开发的第一年,有一个我无法解决的问题,可悲的是,由于冠状病毒,下周我不能问老师。 I have a table with full names and I need to separate them into a first name and a last name I have found some solutions that use SUBSTRING_INDEX but I haven't red about it anywhere yet.我有一个全名的表,我需要将它们分成一个名字和姓氏我已经发现了一些解决方案,使用SUBSTRING_INDEX ,但我还没有红关于它的呢。 I somehow found a way to get the last name, but I just can't get the first name done.我以某种方式找到了获取姓氏的方法,但我就是无法完成名字。 This is how I did the last name:这就是我做姓氏的方式:

SELECT RIGHT(name, LENGTH(name) - INSTR(name, " ")) AS lastname FROM student;

Is there anybody that can help me with this?有没有人可以帮我解决这个问题? Thanks in advance提前致谢

Use substring_index() :使用substring_index()

select substring_index(name, ' ', 1) as first_name,
       substring_index(name, ' ', -1) as last_name

This assumes that the name has only two parts.这假设名称只有两部分。

Use SUBSTRING_INDEX to get everything at the left of the first space as one value, and everything past the first space as other value:使用SUBSTRING_INDEX将第一个空格左侧的所有内容作为一个值,并将第一个空格之后的所有内容作为另一个值:

SELECT substring_index(name, ' ', 1) AS firstname,
       substring_index(name, ' ', -1) AS lastname

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

相关问题 MySQL:从全名字段中提取名字或姓氏 - MySQL: Extracting First Name or Last Name From Full Name Field MYSQL:mysql中的动态列->名字,姓氏到全名: - MYSQL: Dynamic columns in mysql --> first, last name to full name: 在不使用 whereRaw 的情况下使用全名但名字和姓氏在单独的列中搜索和排序 Laravel - Searching and orderby with full name but first name and last name in seperate columns without using whereRaw Laravel 如何将姓氏和名字分成mysql中的两个新列? - How to seperate last name and first names into two new columns in mysql? 在 MySQL 数据库中搜索全名或名字或姓氏,名字和姓氏在单独的列中 - Searching full name or first or last name in MySQL database with first and last name in separate columns 在全名列中搜索名字和姓氏 - Search in Full name column with first and last name mysql 查询。 需要用名字和姓氏搜索全名,反之亦然 - Mysql query. Need search full name with first name and last name and wise versa 连接名字和姓氏以形成全名字段 - Concatenate first name and last name to form a full name field MySQL / RegEXP / 拆分名字和姓氏 - MySQL / RegEXP / Split First Name and Last Name 如何使用 mySQL 中的代码将“名字姓氏”更改为“姓氏,名字”? - How do I change "First Name Last Name" to "Last Name, First Name" using code in mySQL?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM