gpt4 book ai didi

mysql - 删除 mysql 查询中字符串字段上的重音

转载 作者:行者123 更新时间:2023-11-29 02:40:53 25 4
gpt4 key购买 nike

我有以下查询:

SELECT * FROM main_creditperson WHERE name="Irene Olga López"

这将返回:

id         name
366354 Irene Olga López

是否有一种简单的方法可以在查询中执行此操作,以便删除所有重音的结果?

id         name
366354 Irene Olga Lopez

最佳答案

您可以尝试创建一个函数来将重音替换为普通单词。

架构(MySQL v5.6)

CREATE TABLE main_creditperson(id int,name Nvarchar(50));

INSERT INTO main_creditperson VALUES (366354,N'Irene Olga López');


ALTER TABLE main_creditperson
MODIFY name VARCHAR(50) CHARACTER SET utf8mb4
COLLATE utf8mb4_bin;

DROP FUNCTION IF EXISTS fn_remove_accents;
DELIMITER |
CREATE FUNCTION fn_remove_accents( textvalue VARCHAR(10000) ) RETURNS VARCHAR(10000)

BEGIN

SET @textvalue = textvalue;

-- ACCENTS
SET @withaccents = 'ŠšŽžÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÑÒÓÔÕÖØÙÚÛÜÝŸÞàáâãäåæçèéêëìíîïñòóôõöøùúûüýÿþƒ';
SET @withoutaccents = 'SsZzAAAAAAACEEEEIIIINOOOOOOUUUUYYBaaaaaaaceeeeiiiinoooooouuuuyybf';
SET @count = LENGTH(@withaccents);

WHILE @count > 0 DO
SET @textvalue = REPLACE(@textvalue, SUBSTRING(@withaccents, @count, 1), SUBSTRING(@withoutaccents, @count, 1));
SET @count = @count - 1;
END WHILE;

SET @special = '!@#$%¨&*()_+=§¹²³£¢¬"`´{[^~}]<,>.:;?/°ºª+*|\\''';
SET @count = LENGTH(@special);

WHILE @count > 0 do
SET @textvalue = REPLACE(@textvalue, SUBSTRING(@special, @count, 1), '');
SET @count = @count - 1;
END WHILE;

RETURN @textvalue;

END
|
DELIMITER ;

查询#1

SELECT id,fn_remove_accents(name)  name
FROM main_creditperson
WHERE name="Irene Olga López";

| id | name |
| ------ | ---------------- |
| 366354 | Irene Olga Lopez |

View on DB Fiddle


function Reference

关于mysql - 删除 mysql 查询中字符串字段上的重音,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52396205/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com