How to select the first letter of each word from a table cell in MySQL?

穿精又带淫゛_ 提交于 2019-12-24 15:00:40

问题


How can I select the first letter of each word in MySQL using a query?

So this table

+----+----------------------------+
| id | str                        |
+----+----------------------------+
|  1 | Hello my name is MCEmperor |
|  2 | How are you doing?         |
+----+----------------------------+

would return

+----+----------------------------+
| id | str                        |
+----+----------------------------+
|  1 | HmniM                      |
|  2 | Hayd                       |
+----+----------------------------+

I guess it's something with SUBSTRING and LOCATE and maybe I need a loop (to find all spaces or something)...

Is it possible within a query? How should I do that?


回答1:


Maybe you could simply split by space? Use this stored proc : http://forums.mysql.com/read.php?60,78776,148332#msg-148332

You can then retrieve the first letters of each word and use GROUP_CONCAT in a GROUP BY Id to put the letters back into one line per initial text.




回答2:


What you're looking for is a WHERE clause that matches only part of the data in the cell. You can do that like so:

SELECT str 
from (table name) 
WHERE str LIKE 'H%'


来源:https://stackoverflow.com/questions/8313154/how-to-select-the-first-letter-of-each-word-from-a-table-cell-in-mysql

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!