在现有表中将每个单词的第一个字母大写
我有一个现有的表“people_table”,其中包含一个字段full_name
。
许多记录的“full_name”字段填充了错误的大小写。例如'fred Jones'
或'fred Jones'
或'Fred Jones'
。
我可以通过以下方式找到这些错误条目:
SELECT * FROM people_table WHERE full_name REGEXP BINARY '^[a-z]';
如何将找到的每个单词的第一个字母大写?例如,'fred Jones'
变为 'Fred Jones'
。
I have an existing table 'people_table', with a field full_name
.
Many records have the 'full_name' field populated with incorrect casing. e.g. 'fred Jones'
or 'fred jones'
or 'Fred jones'
.
I can find these errant entries with:
SELECT * FROM people_table WHERE full_name REGEXP BINARY '^[a-z]';
How can I capitalize the first letter of each word found? e.g. 'fred jones'
becomes 'Fred Jones'
.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(16)
MySQL 没有函数可以做到这一点,你必须自己编写。在以下链接中有一个实现:
http://joezack。 com/index.php/2008/10/20/mysql-capitalize-function/
为了使用它,首先您需要在数据库中创建该函数。例如,您可以使用 MySQL 查询浏览器(右键单击数据库名称并选择创建新函数)来执行此操作。
创建函数后,您可以使用如下查询更新表中的值:
There's no MySQL function to do that, you have to write your own. In the following link there's an implementation:
http://joezack.com/index.php/2008/10/20/mysql-capitalize-function/
In order to use it, first you need to create the function in the database. You can do this, for example, using MySQL Query Browser (right-click the database name and select Create new Function).
After creating the function, you can update the values in the table with a query like this:
如果您只需要运行一次,并且不想创建函数,则可以执行一些真正的硬编码操作,如下所示:
If you need to run it just one time, and you don't want to create a function, you can do something really-harcoded as:
如果您想将所有单词大写,则需要调用自定义函数。
现在你这样做:
If you want to capitalize all words, it will be needed to invoke a custom function.
Now you do this way:
以下是 Nicholas Thompson 提出的两个有用的函数。您可以将 UC_DELEMITER 的第三个变量设置为 false,将第二个变量设置为“”以表示多个单词的大写。
UC_FIRST
将任何给定字符串大写 - 该函数是 PHP 中 ucfirst 函数的克隆。
UC_DELIMITER 大写并在单词之间添加分隔符
示例:
函数的网页
Here are two useful functions by Nicholas Thompson. You can set the 3rd variable of UC_DELEMITER to false, and the second to " " for the capitalization of more than one word.
UC_FIRST
Capitalize any given string - This function is a clone of the ucfirst function in PHP.
UC_DELIMITER Capitalize with a delimiter in between words
Examples:
Function's Webpage
我尝试了上面的代码,但函数有语法错误,因此无法创建它。如果对任何人有帮助的话,这是为最新版本的 MySQL 写的
I tried the code from above but had syntax errors on function, so could not create it. Wrote this for latest version of MySQL if it helps anyone
简单地 :
Simply :
如果它是一个定时器,则无需创建函数。下面的代码工作得很好:
注意: IF 子句应该等于或大于@num_spaces 的值。当前的 sql 将处理最多 3 个单词。如果需要,您可以添加更多。
No need for creating a function if it is a one timer. The below works just fine:
NOTE: The IF clause should be equal to or more than the value of @num_spaces. The current sql will take care of at max 3 words. You may add more if required.
首先创建一个函数
,然后使用这个查询
firstly create a function
and then use this query
Eric Leroy 的答案是这里最有趣的答案,但它忽略了两件事:
以下是创建函数的更新代码:
通过以下请求:
您将得到:
Answer from Eric Leroy is the most interesting answer here, but it misses two things:
Here is the updated code to create the functions:
With the following request:
You will get:
这可以通过使用 LOWER、UPPER 和 SUBSTRING 函数来完成,请参见下面的示例查询
在 UPDATE 查询中,这将是
This can be done by using LOWER, UPPER and SUBSTRING functions, see below sample query
In an UPDATE query this will be
如果您想避免编写自己的存储函数,可以使用 JSON_TABLE 将文本“分解”为多行(每个单词一个),然后处理每个单词并将它们连接在一起。
If you want to avoid writing your own stored function you can use JSON_TABLE to "explode" the text into multiple rows (one per word), then work on each word and concat them back together.
仅使用 REGEXP_REPLACE 解决方案
结果:
Solution only with REGEXP_REPLACE
Result:
在 phpmyadmin 上,运行此
UPDATE table_name SET Column_Name = LOWER(Column_Name)
然后在显示数据库表数据的html页面中使用css
text-transform: Capitalize;
on phpmyadmin, run this
UPDATE table_name SET Column_Name = LOWER(Column_Name)
then in the html page that displays the data from the database table use css
text-transform: capitalize;
Excel(或谷歌表格)中的 Proper 函数完全可以满足您的需求。
因此,将 mysql 表导出为 CSV 并导出到 Excel(或谷歌表格)中。然后使用
= Proper(*text_to_capitalize *)
将每个单词的第一个字母大写,然后将该 Excel 工作表以 CSV 格式导出回数据库。
The Proper function in Excel (or google sheets does exactly what you want.
So, export your mysql table as CSV and into Excel (or google sheets). Then use the
= Proper(*text_to_capitalize*)
to capitalize the first letter of each word.Then just export that excel sheet as CSV back into your database.
如果您使用 PHP 那么...
If you are using PHP then...