Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/86.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
MySQL选择大写紧跟小写的位置_Mysql_Sql - Fatal编程技术网

MySQL选择大写紧跟小写的位置

MySQL选择大写紧跟小写的位置,mysql,sql,Mysql,Sql,我有一个包含许多名称的表格,格式如下: Max.Example 我想换新的。有一个空格,但我不小心把它换成了空,所以它们都像:MaxMuster 我无法恢复备份或回滚。 我找到的唯一方法是在普通字母后面的大写字母的任何地方插入空格。但是这个命令是什么呢?一个次要的开始是使用REGEXP检查字母后跟大写字母(将其移动到WHERE): 但同样,这不是一个完整的答案,更只是一个想法。一种可怕的暴力方式在这种情况下可能会有所帮助。用一个大箱子找出第一个大写字母的位置: select (case whe

我有一个包含许多名称的表格,格式如下:

Max.Example
我想换新的。有一个空格,但我不小心把它换成了空,所以它们都像:
MaxMuster

我无法恢复备份或回滚。
我找到的唯一方法是在普通字母后面的大写字母的任何地方插入空格。但是这个命令是什么呢?

一个次要的开始是使用REGEXP检查字母后跟大写字母(将其移动到WHERE):


但同样,这不是一个完整的答案,更只是一个想法。

一种可怕的暴力方式在这种情况下可能会有所帮助。用一个大箱子找出第一个大写字母的位置:

select (case when pos is null then field
             else concat(substr(field, 1, pos - 1), '.', substr(field, pos))
from (select (case when ascii(substr(col, 2, 1)) between ascii('A') and ascii('Z') then 2
                   when ascii(substr(col, 3, 1)) between ascii('A') and ascii('Z') then 3
                   when ascii(substr(col, 4, 1)) between ascii('A') and ascii('Z') then 4
                   when ascii(substr(col, 5, 1)) between ascii('A') and ascii('Z') then 5
                   when ascii(substr(col, 6, 1)) between ascii('A') and ascii('Z') then 6
                   when ascii(substr(col, 7, 1)) between ascii('A') and ascii('Z') then 7
                   when ascii(substr(col, 8, 1)) between ascii('A') and ascii('Z') then 8
                   when ascii(substr(col, 9, 1)) between ascii('A') and ascii('Z') then 9
                   when ascii(substr(col, 10, 1)) between ascii('A') and ascii('Z') then 10
              end) as pos, field
      from t
     ) t
我不确定您是否需要相应的
update
语句,或者只需要查找时段位置的逻辑。

尝试以下步骤

create procedure updateName()
begin
  declare cnt, len, val, flag int;
  declare newName, oldName varchar(30);
  select count(*) into cnt from tbl;
  set cnt =cnt-1;
  while cnt >= 0 do
     set flag=0;
     select details into oldName from tbl limit cnt, 1;
     select length(oldname) into len;
     while flag=0 and len > 0 do
        select ascii(substring(oldname, len)) into val;
        if val < 90 then 
          select concat(substring(oldname, 1, len-1), ' ', substring(oldname,len)) into newname;
          update tbl set details = newName where details  = oldname;
          set flag=1;
        end if;
        set len = len - 1;
     end while;
     set cnt = cnt-1;
  end while;

end//
创建过程updateName()
开始
声明cnt、len、val、flag int;
声明newName,oldName varchar(30);
从tbl中选择count(*)进入cnt;
设置cnt=cnt-1;
而cnt>=0 do
设置标志=0;
从tbl limit cnt中选择详细信息到oldName,1;
在len中选择长度(oldname);
当flag=0且len>0时
在val中选择ascii(子字符串(oldname,len));
如果val<90,则
将concat(子字符串(oldname,1,len-1)、“”,子字符串(oldname,len))选择为newname;
更新tbl set details=newName,其中details=oldname;
设置标志=1;
如果结束;
设置len=len-1;
结束时;
设置cnt=cnt-1;
结束时;
结束//
编辑 对于多个caps char 解决多个caps字符的问题

create procedure updateName()
begin
  declare cnt, len, val, flag int;
  declare newName, oldName varchar(30);
  select count(*) into cnt from tbl;
  set cnt =cnt-1;
  while cnt >= 0 do
     set flag=0;
     select details into oldName from tbl limit cnt, 1;
     select length(oldname) into len;
     while len > 1 do
        select ascii(substring(oldname, len)) into val;
        if val < 90 then 
          select concat(substring(oldname, 1, len-1), ' ', substring(oldname,len)) into newname;
          update tbl set details = newName where details  = oldname;
          set oldname=newname;
        end if;
        set len = len - 1;
     end while;
     set cnt = cnt-1;
  end while;

end//
创建过程updateName()
开始
声明cnt、len、val、flag int;
声明newName,oldName varchar(30);
从tbl中选择count(*)进入cnt;
设置cnt=cnt-1;
而cnt>=0 do
设置标志=0;
从tbl limit cnt中选择详细信息到oldName,1;
在len中选择长度(oldname);
而len>1则可以
在val中选择ascii(子字符串(oldname,len));
如果val<90,则
将concat(子字符串(oldname,1,len-1)、“”,子字符串(oldname,len))选择为newname;
更新tbl set details=newName,其中details=oldname;
设置oldname=newname;
如果结束;
设置len=len-1;
结束时;
设置cnt=cnt-1;
结束时;
结束//
在运行此进程之前,请备份您的表


希望这能有所帮助……

这里有一个暴力方法:

select ltrim(
             replace(
             replace(
             replace(
             replace(
             replace(
             replace(
             replace(
             replace(
             replace(
             replace(
             replace(
             replace(
             replace(
             replace(
             replace(
             replace(
             replace(
             replace(
             replace(
             replace(
             replace(
             replace(
             replace(
             replace(
             replace(
             replace(myColumn
                     ,'A',' A')
                     ,'B',' B')
                     ,'C',' C')
                     ,'D',' D')
                     ,'E',' E')
                     ,'F',' F')
                     ,'G',' G')
                     ,'H',' H')
                     ,'I',' I')
                     ,'J',' J')
                     ,'K',' K')
                     ,'L',' L')
                     ,'M',' M')
                     ,'N',' N')
                     ,'O',' O')
                     ,'P',' P')
                     ,'Q',' Q')
                     ,'R',' R')
                     ,'S',' S')
                     ,'T',' T')
                     ,'U',' U')
                     ,'V',' V')
                     ,'W',' W')
                     ,'X',' X')
                     ,'Y',' Y')
                     ,'Z',' Z')
            )
from myTable

这里有一种不同的方法。将联合查询扩展到列的最大长度。它也有改进的余地

select details, group_concat(t2.c1) as new_value  from (

Select details, 
case 
  when n = 1 then substr(details,n,1)
  when ascii(substr(details,n,1)) between ascii ('A') and ascii ('Z') 
      then concat (' ', substr(details,n,1))
  else substr(details,n,1)
end as c1

,n
FROM tbl, 

  (select 1 as n union all
select 2 union all
select 3 union all
select 4 union all
select 5 union all
select 6 union all
select 7 union all
select 8 union all
select 9 union all
select 10
    )
  as tbl_1 

where substr(details,n,1) is not null

) as t2
group by details

我根据马克·班尼斯特的暴力脚本找到了答案

UPDATE TABLE
SET COLUMN = ltrim(
             replace(
             replace(
             replace(
             replace(
             replace(
             replace(
             replace(
             replace(
             replace(
             replace(
             replace(
             replace(
             replace(
             replace(
             replace(
             replace(
             replace(
             replace(
             replace(
             replace(
             replace(
             replace(
             replace(
             replace(
             replace(
             replace(COLUMN
                      ,'zA','z A')
                     ,'zB','z B')
                     ,'zC','z C')
                     ,'zD','z D')
                     ,'zE','z E')
                     ,'zF','z F')
                     ,'zG','z G')
                     ,'zH','z H')
                     ,'zI','z I')
                     ,'zJ','z J')
                     ,'zK','z K')
                     ,'zL','z L')
                     ,'zM','z M')
                     ,'zN','z N')
                     ,'zO','z O')
                     ,'zP','z P')
                     ,'zQ','z Q')
                     ,'zR','z R')
                     ,'zS','z S')
                     ,'zT','z T')
                     ,'zU','z U')
                     ,'zV','z V')
                     ,'zW','z W')
                     ,'zX','z X')
                     ,'zY','z Y')
                     ,'zZ','z Z')
            );
这对任何事情都有效

MaxExample = Max Example
MaxExampleTest = Max Example Test
MaxExampleTestTT = Max Example Test TT

对所有字母重复26次。

是否只有一个点?@yvytty没有区别。没关系。只有一列。这些字段是普通文本字段,可能还有其他点,但如果用空格替换这些字段,则不会有问题。该表还有其他列,但我只是对名称有问题。它是安装正则表达式模块的选项吗?在这种情况下,修复起来相当简单,否则不是不可能的,而是有点麻烦。@WoLpH我希望它只与普通SQL一起使用。但如果没有其他方法。好的,它会告诉我是否有一个,但它不会决定我的表中是否发生了多个BLE情况。就像你的方法一样,但当名称有多个大写字母时它会断开。例如:
asminiddletest
is then
asminiddletest
我想我需要使用Mark Bannister蛮力方法。@user2436057请尝试编辑的方法,它适用于任意数量的caps char。我刚刚删除了标志,停在1个字符处。。检查编辑后的答案。谢谢,但它仍然有一个双大写字母的问题,我认为BruteForce是“asmindiddletesttt”给出“asminfiddle Test T”而不是“asminfiddle Test TT”的最好方式。这个过程会在发现caps char时分裂,这种行为甚至会在蛮力方法中出现……这很好,我喜欢它。我现在只需要修复它,这样它就不会在前面留下空格,因此thad在名称为“MaxExampleMISTER”左右时会留下空格。@user2436057:开始时的LTRIM应该处理前导空格,但处理多个连续大写字母可能会有更大的问题-我建议使用regexp来识别它们,并分别进行更新。这既非常快速,又完美无瑕。这不是最整洁的,但工作质量和速度都很好。
MaxExample = Max Example
MaxExampleTest = Max Example Test
MaxExampleTestTT = Max Example Test TT