MySQL – 在现有表格中大写每个单词的第一个字母

我有一个现有的表'people_table',与字段full_name

许多logging具有填充不正确的shell的“full_name”字段。 例如'fred Jones''fred jones''Fred jones'

我可以find这些错误的条目:

 SELECT * FROM people_table WHERE full_name REGEXP BINARY '^[az]'; 

我怎样才能把每个单词的第一个字母大写? 例如'fred jones'成为'Fred Jones'

没有MySQL的function,你必须自己写。 在下面的链接中有一个实现:

http://joezack.com/index.php/2008/10/20/mysql-capitalize-function/

为了使用它,首先需要在数据库中创build函数。 例如,您可以使用MySQL Query Browser(右键单击数据库名称并selectCreate new Function)来执行此操作。

创build函数后,可以使用如下查询更新表中的值:

 UPDATE users SET name = CAP_FIRST(name); 

如果你只需要运行一次,而不想创build一个函数,那么你可以做一些真正的编码:

 UPDATE people_table SET full_name = LOWER(full_name); UPDATE people_table SET full_name = CONCAT(UPPER(SUBSTR(full_name,1,1)),LOWER(SUBSTR(full_name,2))); UPDATE people_table SET full_name = REPLACE(full_name,' a',' A'); UPDATE people_table SET full_name = REPLACE(full_name,' b',' B'); UPDATE people_table SET full_name = REPLACE(full_name,' c',' C'); UPDATE people_table SET full_name = REPLACE(full_name,' d',' D'); UPDATE people_table SET full_name = REPLACE(full_name,' e',' E'); UPDATE people_table SET full_name = REPLACE(full_name,' f',' F'); UPDATE people_table SET full_name = REPLACE(full_name,' g',' G'); UPDATE people_table SET full_name = REPLACE(full_name,' h',' H'); UPDATE people_table SET full_name = REPLACE(full_name,' i',' I'); UPDATE people_table SET full_name = REPLACE(full_name,' j',' J'); UPDATE people_table SET full_name = REPLACE(full_name,' k',' K'); UPDATE people_table SET full_name = REPLACE(full_name,' l',' L'); UPDATE people_table SET full_name = REPLACE(full_name,' m',' M'); UPDATE people_table SET full_name = REPLACE(full_name,' n',' N'); UPDATE people_table SET full_name = REPLACE(full_name,' o',' O'); UPDATE people_table SET full_name = REPLACE(full_name,' p',' P'); UPDATE people_table SET full_name = REPLACE(full_name,' q',' Q'); UPDATE people_table SET full_name = REPLACE(full_name,' r',' R'); UPDATE people_table SET full_name = REPLACE(full_name,' s',' S'); UPDATE people_table SET full_name = REPLACE(full_name,' t',' T'); UPDATE people_table SET full_name = REPLACE(full_name,' u',' U'); UPDATE people_table SET full_name = REPLACE(full_name,' v',' V'); UPDATE people_table SET full_name = REPLACE(full_name,' w',' W'); UPDATE people_table SET full_name = REPLACE(full_name,' x',' X'); UPDATE people_table SET full_name = REPLACE(full_name,' y',' Y'); UPDATE people_table SET full_name = REPLACE(full_name,' z',' Z'); 

如果你想大写所有单词,将需要调用一个自定义函数

 -- may help: -- DROP function if exists capitalize; DELIMITER $$ CREATE FUNCTION `capitalize`(s varchar(255)) RETURNS varchar(255) BEGIN declare c int; declare x varchar(255); declare y varchar(255); declare z varchar(255); set x = UPPER( SUBSTRING( s, 1, 1)); set y = SUBSTR( s, 2); set c = instr( y, ' '); while c > 0 do set z = SUBSTR( y, 1, c); set x = CONCAT( x, z); set z = UPPER( SUBSTR( y, c+1, 1)); set x = CONCAT( x, z); set y = SUBSTR( y, c+2); set c = INSTR( y, ' '); end while; set x = CONCAT(x, y); return x; END$$ DELIMITER ; 

现在你这样做了:

  UPDATE mytable SET thefield = capitalize(thefield); 

我尝试了上面的代码,但function上有语法错误,所以无法创build它。 写了这个最新版本的MySQL,如果它可以帮助任何人

 CREATE FUNCTION `CAP_FIRST`(input VARCHAR(255)) RETURNS varchar(255) CHARSET latin1 DETERMINISTIC BEGIN DECLARE len INT; DECLARE i INT; DECLARE charnum INT; declare SortedName varchar(255); SET len = CHAR_LENGTH(input); SET input = LOWER(input); SET i = 1; set charnum = 1; set SortedName = ''; WHILE (i <= len) DO if charnum = 1 then set SortedName = concat(SortedName,upper(mid(input,i,1))); set charnum = charnum + 1; else if mid(input,i,1) = ' ' then set SortedName = concat(SortedName,' '); set charnum = 1; else set SortedName = concat(SortedName,mid(input,i,1)); set charnum = charnum + 1; end if; end if; SET i = i + 1; END WHILE; RETURN SortedName; END 

这是Nicholas Thompson的两个有用的function。 您可以将UC_DELEMITER的第三个variables设置为false,将第二个variables设置为多个单词的大写。

UC_FIRST大写任何给定的string – 该函数是PHP中的ucfirst函数的克隆。

 DROP FUNCTION IF EXISTS UC_FIRST; CREATE FUNCTION UC_FIRST(oldWord VARCHAR(255)) RETURNS VARCHAR(255) RETURN CONCAT(UCASE(SUBSTRING(oldWord, 1, 1)),SUBSTRING(oldWord, 2)); 

UC_DELIMITER在单词之间使用分隔符进行大写

 DROP FUNCTION IF EXISTS UC_DELIMITER; DELIMITER // CREATE FUNCTION UC_DELIMITER( oldName VARCHAR(255), delim VARCHAR(1), trimSpaces BOOL ) RETURNS VARCHAR(255) BEGIN SET @oldString := oldName; SET @newString := ""; tokenLoop: LOOP IF trimSpaces THEN SET @oldString := TRIM(BOTH " " FROM @oldString); END IF; SET @splitPoint := LOCATE(delim, @oldString); IF @splitPoint = 0 THEN SET @newString := CONCAT(@newString, UC_FIRST(@oldString)); LEAVE tokenLoop; END IF; SET @newString := CONCAT(@newString, UC_FIRST(SUBSTRING(@oldString, 1, @splitPoint))); SET @oldString := SUBSTRING(@oldString, @splitPoint+1); END LOOP tokenLoop; RETURN @newString; END// DELIMITER ; 

例子:

 SELECT UC_DELIMITER('eric-leroy','-',TRUE); Eric-Leroy 

function的网页

 DELIMITER $$ CREATE FUNCTION `capitalize`(s varchar(255)) RETURNS varchar(255) BEGIN declare c int; declare x varchar(255); declare y varchar(255); declare z varchar(255); set x = UPPER( SUBSTRING( s, 1, 1)); set y = lower(SUBSTR( s, 2)); set c = instr( y, ' '); while c > 0 do set z = SUBSTR( y, 1, c); set x = CONCAT( x, z); set z = UPPER( SUBSTR( y, c+1, 1)); set x = CONCAT( x, z); set y = SUBSTR( y, c+2); set c = INSTR( y, ' '); end while; set x = CONCAT(x, y); return x; END$$ DELIMITER ; Create above function to set First character to capital of each words 

在phpmyadmin上运行这个UPDATE table_name SET Column_Name = LOWER(Column_Name)然后在显示数据库表的数据的html页面中使用css text-transform: capitalize;

在Excel(或谷歌工作表正确的function正是你想要的。

所以,将你的mysql表格导出为CSV格式,然后导入Excel(或者Google表格)。 然后使用= Proper(*text_to_capitalize*)来大写每个单词的首字母。

然后,将该Excel表单导出为CSV数据库。

如果您使用的PHP然后…

 try{ $con = new PDO("mysql:host=localhost;dbname=dbasename", "root", ""); } catch(PDOException $e){ echo "error" . $e-getMessage(); } $select = $con->prepare("SELECT * FROM table"); $select->setFetchMode(PDO::FETCH_ASSOC); $select->execute(); while($data=$select->fetch()) { $id = $data['id']; $column = $data['column']; $column = ucwords(strtolower($column)); // Capitalize each word $update = $con->prepare("UPDATE table SET column=:column WHERE id='$id'"); $update->bindParam(':column', $column); $update->execute(); }