Mysql 提取特定列中每个单词的第一个字母

问题描述 投票:0回答:7

我想在表中创建一个缩写词列。我想从“名称”列中获取每个单词的第一个字母,将其大写,然后将所有字母连接到“首字母缩略词”列中。

有什么简单的方法来抓取第一个字母吗?

mysql acronym
7个回答
8
投票

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

  • 函数
    initials
    执行实际工作,您必须指定正则表达式
  • 函数
    acronym
    仅保留字母数字字符

(如有必要,请在输出上使用

upper
lower
ucase
功能) .

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!');

输出:

抓住

示例2:

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

输出:

oe啊啊啊


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 ;

1
投票

我知道这有点晚了,但我想为那些创建视图或 .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

这是两个步骤,您必须为字符串中预期出现的每个单词添加一个条件 substring() 行,但这只是@spaces 比较值的复制、粘贴和增量。然而,我这样做的要求可能比某些要求宽松一些。无论如何,它可以工作并且不会导致明显的速度问题。


0
投票

这应该将所有第一个字母放入结果集中:

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

我认为,将它们全部连接成一个首字母缩写词需要某种程序。我认为这不能通过声明来完成。


0
投票

您是指吗?


0
投票
SELECT REGEXP_REPLACE( ' Bart Van Eynde', ' (.)[^ ]+', '\\1' ); -- 'BVE'
  • 开始之前,在字符串前添加一个空格
  • ' (.)[^ ]+' = 开始寻找空格+一些东西(并保存它)+如果不是空格则忽略其余的
  • '\1' 只写回'东西'

在查询中:

SELECT UPPER( REGEXP_REPLACE( CONCAT(' ', col1), ' (.)[^ ]+', '\\1' ) ) from table1;

0
投票

这适用于 3 个字母的术语,根据需要修改更长的术语:

Select concat(left(Term,1),left(substring_index(Term,' ',-2),1),left(substring_index(Term,' ',-1),1)) from Table
© www.soinside.com 2019 - 2024. All rights reserved.