【问题标题】:Mysql extract first letter of each word in a specific columnMysql提取特定列中每个单词的首字母
【发布时间】:2011-12-29 20:06:28
【问题描述】:

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

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

【问题讨论】:

  • 您是否在问是否有任何简单的方法可以只使用 SQL 来做到这一点?
  • 是的,或者任何快速简便的方法。我有一个包含 170 多行的表,我在其中添加了一个首字母缩写词列。我只是在寻找一种快速获取“名称”并使用每个单词的首字母作为首字母缩写词的方法。
  • 我认为你需要一些更高级的编程结构来做一些不会让你想太多的事情。

标签: mysql acronym


【解决方案1】:

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

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

输出:

捕捉

示例2:

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

输出:

oeAaaae

【讨论】:

  • 你能解释一下我如何安装这个功能吗?当我将它输入到 PhpMyAdmin 的 SQL 部分时,它返回错误“MySQL 返回了一个空结果集(即零行)。”
【解决方案2】:

这种字符串操作不是 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 ;

【讨论】:

    【解决方案3】:

    这应该将所有首字母放入结果集中:

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

    我认为,将它们全部连接成一个首字母缩略词需要某种程序。我不认为它可以在一个声明中完成。

    【讨论】:

    • 这个只返回第一个单词的第一个字母?对吗?
    • 然后改成SELECT UPPER(CONCAT(SUBSTR(col1, 0, 1), SUBSTR(col2, 0, 1))) as acronym FROM the_table
    • 如下所述,如果 COL1 包含“My Big Fat String”,我需要 ARNM 为“MBFS”...单个列中每个单词的第一个字母。
    • @SamGrant - 啊。我误解了你的要求。与 MS SQL 不同,MySQL 没有在分隔符处拆分字符串的功能。您肯定需要编写一个程序。如果这是一次性的事情,您可能最好将数据检索到程序中,计算首字母缩写词,然后将其插入回数据库中。如果你想只在 MySQL 中做这件事,有一个关于这个主题的讨论线程 here on the MySQL forums 可以让你开始。
    【解决方案4】:

    你的意思是LEFTUPPER吗?

    【讨论】:

    • 因为字符串可能是 My Big Fat String,LEFT 只会返回 'M',我需要 "MBFS"...
    【解决方案5】:

    我知道这对游戏来说有点晚了,但我想为那些创建视图或 .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 比较值的复制、粘贴和增量。但是,我对这样做的要求可能比某些要求要宽松一些。无论如何,它可以工作并且不会导致明显的速度问题。

    【讨论】:

      【解决方案6】:
      SELECT REGEXP_REPLACE( ' Bart Van Eynde', ' (.)[^ ]+', '\\1' ); -- 'BVE'
      
      • 在开始之前在您的字符串前添加一个空格
      • ' (.)[^ ]+' = 开始寻找空间 + 一些东西(并保存)+ 如果不是空间则忽略其余部分
      • '\\1' 只写回'something'

      在查询中:

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-06-05
        • 2018-04-01
        • 2020-08-30
        • 2017-08-02
        • 2013-12-15
        • 1970-01-01
        • 2016-12-05
        • 2015-11-10
        相关资源
        最近更新 更多