简体   繁体   English

SQL:多个字符串替换(在第二个大写字母开头的大写字母前面添加一个空格)

[英]SQL : Multiple String Replace (Add a space in front of capital letter(s) starting with 2nd capital letter)

I've updated the Name_Table.column_2 data with wrong strings. 我用错误的字符串更新了Name_Table.column_2数据。 Instead of 'John Smith',I've updated with 'JohnSmith' . 而不是'John Smith',我更新了'JohnSmith'

Now i would like to replace multiple strings,For Example : 'JohnSmith' as 'John Smith' , 'JohnDoe' as 'John Doe' etc. 现在我想替换多个字符串,例如: 'JohnSmith''John Smith''JohnDoe''John Doe'等。

I'm not familiar with SQL , can some one help me on how to replace multiple strings all at once. 我不熟悉SQL ,有人可以帮我解决如何一次更换multiple strings

#Name_Table

Column_1      Column_2
1             JohnSmith
2             JohnSmith
3             JohnDoe
4             JohnSmith
5             WayneRooney
6             JohnDoe
7             WayneRooney
8             JohnSmith
9             WayneRooney
10            JohnDoe

I dont know single query for this situation, but try below method to solve your problem. 我不知道这种情况的单一查询,但尝试以下方法来解决您的问题。 I'm sure it works fine for you. 我相信它对你有用。

$sel = mysql_query('SELECT * FROM Name_Table;');
while($row = mysqli_fetch_array($sel)){
    $string = $row['Column_2'];
    $count = strlen($string);
    $strings = array();
    $i = 0;
    $ii = 0;

    while($i < $count)
    {
        $char = $string{$i};
        if(ereg("[A-Z]", $char, $val)){
            $ii++;
            $s = '';
            $s .= $char;
        } else {
            $s .= $char;
        }
        $strings[$ii] = $s;
        $i++;
    }
    $name_with_space = implode(' ',$strings);
    mysql_query('UPDATE Name_Table SET Column_2="'.$name_with_space.'" WHERE Column_1='.$row['Column_1']);
}

if your names are always gonna be in the format of FirstnameLastname you can do a custom function like this 如果你的名字总是采用FirstnameLastname的格式,你可以做这样的自定义函数

CREATE FUNCTION breakup_name (fullname varchar(50))
RETURNS VARCHAR(50)
BEGIN
  SET @fullname = fullname,@newname='',@letter='',@capcount=0,@space='';
  WHILE LENGTH(@fullname)>0 DO
    SET @letter = LEFT(@fullname,1);
    SET @space = '';
    IF @letter RLIKE BINARY '[A-Z]' THEN
      SET @capcount = @capcount+1;
      IF @capcount >= 2 THEN SET @space = ' '; END IF;
    END IF;
    SET @newname = CONCAT(@newname,@space,@letter);
    SET @fullname = RIGHT(@fullname,LENGTH(@fullname)-1);
  END WHILE;
  RETURN @newname;
END/

then use an UPDATE like this. 然后像这样使用UPDATE。

UPDATE table1 SET column_2 = breakup_name(column_2);

sqlfiddle sqlfiddle

PS In the sqlfiddle I used / as delimiter, you'll have to change that according to your delimiter. PS在sqlfiddle中我用/作为分隔符,你必须根据你的分隔符改变它。

The above function will add a space in front of a capital letters (starting with the 2nd capital letter), so for example if you had TommyJaeSmith , it'll return Tommy Jae Smith . 上面的函数将在大写字母前面添加一个空格(以第二个大写字母开头),例如,如果你有TommyJaeSmith ,它将返回Tommy Jae Smith

The logic of this function is very simple, it loops through and looks at each letter. 这个函数的逻辑很简单,它遍历并查看每个字母。 If the letter is a capital letter it increments a capcount, if the capcount is greater than or equal to 2 (meaning 2nd capital letter or after) it adds a space... to be concatenated in front of the letter. 如果字母是大写字母,则会增加一个帐数,如果帐数大于或等于2(表示第二个大写字母或之后),则会在字母前面添加一个空格......。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM