开发者

Comma separated values in mysql

I have a column:

Name
J开发者_StackOverflow社区ohn,Doe

I want the output from select as

First name Last name
John       Doe

How to remove comma separated data in mysql?


I would say that this breaks first normal form. Don't store two data items in a single column. If you want two items, use two columns.

If you must, please refer to this documentation. As you can see, MySQL doesn't have a string split function.


You can try below query to do that,

select SUBSTRING_INDEX('John,Doe', ',', 1) as str1, SUBSTRING_INDEX('John,Doe', ',', -1) as str2


SELECT
'FirstName, MiddleName, LastName',
SUBSTRING_INDEX('FirstName, MiddleName, LastName', ',', 1) 
    AS `First Name`,
SUBSTRING_INDEX(SUBSTRING_INDEX('FirstName, MiddleName, LastName', ',', 2), ',', -1) 
    AS  `Middle Name`,
SUBSTRING_INDEX(SUBSTRING_INDEX('FirstName, MiddleName, LastName', ',', 3), ',', -1) 
    AS  `Last Name`

See output of this query here.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜