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