2011-12-29 56 views
1

我想创建一个表中的缩写列。我想抓住'name'列中每个单词的第一个字母,将其大写,然后连接成'首字母缩写词'列。Mysql的提取每个单词的第一个字母在一个特定的列

任何简单的方法抢第一个字母?

+0

你问是否有简单的方法来用SQL来做到这一点? – Bill 2011-12-29 20:08:50

+0

是的,或者任何快速简单的方法。我有一个有170多行的表格,我添加了一个首字母缩写词列。我只是寻找一个快速的方法来抓住一个'名字',并用每个单词的首字母作为首字母缩写词。 – 2011-12-29 20:14:53

+0

我认为你需要一些更高层次的编程结构来完成那些不会让你的思想过度扭曲的事情。 – Bill 2011-12-29 20:17:16

回答

6

这里是一个“改进”的功能,只允许通过正则表达式来过滤想要的字符。

  • 功能initials做实际工作,你必须指定正则表达式
  • 功能acronym做的工作,使字母数字字符仅

(在使用upperlowerucase功能输出如果需要) 。

delimiter $$ 
drop function if exists `initials`$$ 
CREATE FUNCTION `initials`(str text, expr text) RETURNS text CHARSET utf8 
begin 
    declare result text default ''; 
    declare buffer text default ''; 
    declare i int default 1; 
    if(str is null) then 
     return null; 
    end if; 
    set buffer = trim(str); 
    while i <= length(buffer) do 
     if substr(buffer, i, 1) regexp expr then 
      set result = concat(result, substr(buffer, i, 1)); 
      set i = i + 1; 
      while i <= length(buffer) and substr(buffer, i, 1) regexp expr do 
       set i = i + 1; 
      end while; 
      while i <= length(buffer) and substr(buffer, i, 1) not regexp expr do 
       set i = i + 1; 
      end while; 
     else 
      set i = i + 1; 
     end if; 
    end while; 
    return result; 
end$$ 

drop function if exists `acronym`$$ 
CREATE FUNCTION `acronym`(str text) RETURNS text CHARSET utf8 
begin 
    declare result text default ''; 
    set result = initials(str, '[[:alnum:]]'); 
    return result; 
end$$ 
delimiter ; 

例1:

select acronym('Come Again? That Cant Help!'); 

输出:

CATCH

例2:

select initials('Come Again? That Cant Help!', '[aeiou]'); 

输出:

oeAaaae

+0

您能解释更多关于如何安装此功能的信息吗?当我将它输入到PhpMyAdmin的SQL部分时,它会返回错误“MySQL返回一个空结果集(即零行)”。 – 2014-02-07 08:22:36

0

这应该让所有的第一个字母到结果集:

SELECT UPPER(SUBSTR(name, 0, 1)) FROM the_table 

他们都串联成一个单一的缩写会,我认为,需要某种形式的过程。我认为这不能在声明中完成。

+0

这只返回第一个单词的第一个字母?对? – 2011-12-29 20:17:13

+0

然后将其改为'SELECT UPPER(CONCAT(SUBSTR(col1,0,1),SUBSTR(col2,0,1)))作为首字母缩写词FROM the_table' – piotrekkr 2011-12-29 20:20:14

+0

如下所述,如果COL1包含“我的大胖子字符串”,我需要ACRNM成为“MBFS”......单列中每个单词的第一个字母。 – 2011-12-29 20:27:07

0

你的意思LEFTUPPER?这种

+0

由于字符串可能是我的大胖字符串,只有LEFT会返回'M',我需要“MBFS”... – 2011-12-29 20:18:25

1

字符串操作是不是有什么SQL是专为,除非你想为它编写存储过程或UDF。

SQL是不是真的适合这种字符串操作。您可以莫名其妙但你为什么要当更好的工具都可以在其他地方?我在Google上进行了长时间的搜索,发现查询语句,但我不能。只需使用以下函数即可实现您想要的功能。

drop function if exists initials; 
delimiter || 
create function initials(str text) returns text 
begin 
    declare result text default ''; 
    declare i int default 1; 

    if(str is null) then 
     return null; 
    end if; 

    set result = upper(substr(str, 1, 1)); 

    while(i <= length(str)) do 
     if (substring(str, i, 1) = ' ') 
     then 
      set result = concat(result, upper(substr(str, i+1, 1))); 
     end if; 
     set i = i + 1; 
    end while; 

    return ucase(result); 
end; 
delimiter ; 
0

我知道这是一个有点晚了比赛,但我想提供了这样做,对于那些你创建视图的非功能的方式或一个 。定期使用SQL文件:

SELECT 
    @spaces:= length(fi.FacilityName) - length(replace(fi.FacilityName,' ','')) as spaces, 
    concat(left(fi.FacilityName,1), 
     if(@spaces > 0, substring(fi.FacilityName,@pos:=locate(' ',fi.FacilityName)+1,1),''), 
     if(@spaces > 1, substring(fi.FacilityName,@pos:=locate(' ',fi.FacilityName, @pos)+1,1),''), 
     if(@spaces > 2, substring(fi.FacilityName,@pos:=locate(' ',fi.FacilityName,@pos)+1,1),''), 
     if(@spaces > 3, substring(fi.FacilityName,@pos:=locate(' ',fi.FacilityName,@pos)+1,1),''), 
     if(@spaces > 4, substring(fi.FacilityName,@pos:=locate(' ',fi.FacilityName,@pos)+1,1),'')) as initials 
from facilityInfo fi 

这两个步骤,你必须有一个条件子()线,用于对您预计在字符串中能够字,但是这仅仅是一个复制,粘贴,和增量@spaces的比较值。然而,我这样做的要求可能比一些松散一点。无论如何,它的工作原理并不会造成明显的速度问题。

相关问题