【发布时间】:2011-08-29 08:17:48
【问题描述】:
我有一个包含数千条记录的数据库,我需要删除其中一个字段以确保它只包含某些字符(字母数字、空格和单引号)。我可以使用什么 SQL 从整个数据库中的该字段中删除任何其他字符(例如斜杠等)?
【问题讨论】:
我有一个包含数千条记录的数据库,我需要删除其中一个字段以确保它只包含某些字符(字母数字、空格和单引号)。我可以使用什么 SQL 从整个数据库中的该字段中删除任何其他字符(例如斜杠等)?
【问题讨论】:
查看LIB_MYSQLUDF_PREG,它需要编译到 MySQL 服务器中,但具有高级正则表达式工具,例如 preg_replace,它将帮助您完成任务。
【讨论】:
update mytable
set FieldName = REPLACE(FieldName,'/','')
这是一个很好的起点。
【讨论】:
Replace() 函数是首选。但是,特殊字符有时可能很难在控制台中编写。对于那些您可以将 Replace 与 Char() 函数结合使用。
例如删除€
Update products set description = replace(description, char(128), '');
你可以找到所有Ascii values here
理想情况下,您可以使用正则表达式来查找所有特殊字符,但显然是that's not possible with MySQL.
除此之外,您还需要通过自己喜欢的脚本语言运行它。
【讨论】:
这也可能有用。
首先你必须知道数据库和/或表的字符集。例如,假设您有一个 UTF-8 环境,并且您想从字段中删除/删除带圆圈的注册符号、带圆圈的版权符号和注册商标符号等符号,然后通过 bing、yahoo 或 google 在互联网上搜索这些符号在 UTF-8 系统中的十六进制码值:
符号 Utf-8 十六进制 ======= ========= 圈出版权C2A9 圈出注册C2AE 商标(即 TM)E284A2然后,使用 hex / unhex 工具与 replace 函数一起从表 t1 中为字段 f1 擦洗 select sql,很可能如下所示:
SELECT
cast(unhex(replace(replace(replace(hex(f1),'C2A9',''),'C2AE',''),'E284A2','')) AS char) AS cleanf1
FROM t1
;
上面,注意要擦洗/清理的原始字段是f1,表是t1,输出头是cleanf1。 “as char”转换是必要的,因为没有它,我测试的 mysql 5.5.8 正在返回 blob。希望这会有所帮助
【讨论】:
详细说明 Vinnies 的答案...您可以使用以下内容(注意最后两个语句中的转义...
update table set column = REPLACE(column,"`","");
update table set column = REPLACE(column,"~","");
update table set column = REPLACE(column,"!","");
update table set column = REPLACE(column,"@","");
update table set column = REPLACE(column,"#","");
update table set column = REPLACE(column,"$","");
update table set column = REPLACE(column,"%","");
update table set column = REPLACE(column,"^","");
update table set column = REPLACE(column,"&","");
update table set column = REPLACE(column,"*","");
update table set column = REPLACE(column,"(","");
update table set column = REPLACE(column,")","");
update table set column = REPLACE(column,"-","");
update table set column = REPLACE(column,"_","");
update table set column = REPLACE(column,"=","");
update table set column = REPLACE(column,"+","");
update table set column = REPLACE(column,"{","");
update table set column = REPLACE(column,"}","");
update table set column = REPLACE(column,"[","");
update table set column = REPLACE(column,"]","");
update table set column = REPLACE(column,"|","");
update table set column = REPLACE(column,";","");
update table set column = REPLACE(column,":","");
update table set column = REPLACE(column,"'","");
update table set column = REPLACE(column,"<","");
update table set column = REPLACE(column,",","");
update table set column = REPLACE(column,">","");
update table set column = REPLACE(column,".","");
update table set column = REPLACE(column,"/","");
update table set column = REPLACE(column,"?","");
update table set column = REPLACE(column,"\\","");
update table set column = REPLACE(column,"\"","");
【讨论】:
我为此创建了简单的函数
DROP FUNCTION IF EXISTS `regex_replace`$$
CREATE FUNCTION `regex_replace`(pattern VARCHAR(1000),replacement VARCHAR(1000),original VARCHAR(1000)) RETURNS VARCHAR(1000) CHARSET utf8mb4
DETERMINISTIC
BEGIN
DECLARE temp VARCHAR(1000);
DECLARE ch VARCHAR(1);
DECLARE i INT;
SET i = 1;
SET temp = '';
IF original REGEXP pattern THEN
loop_label: LOOP
IF i>CHAR_LENGTH(original) THEN
LEAVE loop_label;
END IF;
SET ch = SUBSTRING(original,i,1);
IF NOT ch REGEXP pattern THEN
SET temp = CONCAT(temp,ch);
ELSE
SET temp = CONCAT(temp,replacement);
END IF;
SET i=i+1;
END LOOP;
ELSE
SET temp = original;
END IF;
RETURN temp;
END
用法示例:
SELECT <field-name> AS NormalText, regex_replace('[^A-Za-z0-9 ]', '', <field-name>)AS RegexText FROM
<table-name>
【讨论】:
我的 MySQL 版本没有 REGEXP_REPLACE()。我使用了以下两种解决方法: 1.删除指定字符(如果你知道要删除哪些字符)
create function fn_remove_selected_characters
(v_input_string varchar(255),
v_unacceptable_characters varchar(255))
RETURNS varchar(255)
BEGIN
-- declare variables
declare i int;
declare unacceptable_values varchar(255);
declare this_character char(1);
declare output_string varchar(255);
declare input_length int;
declare boolean_value int;
declare space varchar(3);
-- Set variable values
set input_length = char_length(v_input_string);
set i = 0;
set unacceptable_values = v_unacceptable_characters;
set output_string = '';
set boolean_value = 0;
set space = 'no';
begin
-- Leave spaces if they aren't in the exclude list
if instr( unacceptable_values, ' ') = 0 then
begin
while i < input_length do
SET this_character = SUBSTRING( v_input_string, i, 1 );
-- If the current character is a space,
-- then concatenate a space to the output
-- Although it seems redundant to explicitly add a space,
-- SUBSTRING() equates a space to the empty string
if this_character = ' ' then
set output_string = concat(output_string, ' ');
-- if the current character is not a space, remove it if it's unwanted
elseif instr(unacceptable_values, this_character) then
set output_string = concat(output_string, '');
-- otherwise include the character
else set output_string = concat(output_string, this_character);
end if;
set i = i + 1;
end while;
end;
else
begin
while i < input_length do
begin
SET this_character = SUBSTRING( v_input_string, i, 1 );
if instr(unacceptable_values, this_character) > 0 then
set output_string = concat(output_string, '');
else set output_string = concat(output_string, this_character);
end if;
end;
set i = i + 1;
end while;
end;
end if;
end;
RETURN output_string;
create function fn_preserve_selected_characters
(v_input_string varchar(255),
v_acceptable_characters varchar(255))
returns varchar(255)
begin
declare i int;
declare acceptable_values varchar(255);
declare this_character char(1);
declare output_string varchar(255);
declare input_length int;
declare boolean_value int;
declare space varchar(3);
set input_length = char_length(v_input_string);
set i = 0;
set acceptable_values = v_acceptable_characters;
set output_string = '';
set boolean_value = 0;
set space = 'no';
begin
-- check for existence of spaces
if instr( acceptable_values, ' ') then
begin
while i < input_length do
-- SUBSTRING() treats spaces as empty strings
-- so handle them specially
SET this_character = SUBSTRING( v_input_string, i, 1 );
if this_character = ' ' then
set output_string = concat(output_string, ' ');
elseif instr(acceptable_values, this_character) then
set output_string = concat(output_string, this_character);
else set output_string = concat(output_string, '');
end if;
set i = i + 1;
end while;
end;
-- if there are no spaces in input string
-- then this section is complete
else
begin
while i <= input_length do
SET this_character = SUBSTRING( v_input_string, i, 1 );
-- if the current character exists in the punctuation string
if LOCATE( this_character, acceptable_values ) > 0 THEN
set output_string = concat(output_string, this_character);
end if;
set i = i+1;
end while;
end;
end if;
end;
RETURN output_string;
【讨论】:
没有正则表达式替换。使用以下代码将所有特殊字符替换为“-”。
UPDATE <table> SET <column> = 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 (REPLACE (REPLACE (REPLACE (REPLACE (REPLACE (<column>, '/', '-'), ',', '-'), '.', '-'), '<', '-'), '>', '-'), '?', '-'), ';', '-'), ':', '-'), '"', '-'), "'", '-'), '|', '-'), '\\', '-'), '=', '-'), '+', '-'), '*', '-'), '&', '-'), '^', '-'), '%', '-'), '$', '-'), '#', '-'), '@', '-'), '!', '-'), '~', '-'), '`', '-'), '', '-'), '{', '-' ), '}', '-' ), '[', '-' ), ']', '-' ), '(', '-' ), ')', '-' )
代码格式化
UPDATE
<table>
SET
<column> =
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
(
REPLACE
(
REPLACE
(
REPLACE
(
REPLACE
(
REPLACE
(<column>, '/', '-'),
',',
'-'
),
'.',
'-'
),
'<',
'-'
),
'>',
'-'
),
'?',
'-'
),
';',
'-'
),
':',
'-'
),
'"',
'-'
),
"'",
'-'
),
'|',
'-'
),
'\\',
'-'
),
'=',
'-'
),
'+',
'-'
),
'*',
'-'
),
'&',
'-'
),
'^',
'-'
),
'%',
'-'
),
'$',
'-'
),
'#',
'-'
),
'@',
'-'
),
'!',
'-'
),
'~',
'-'
),
'`',
'-'
),
'',
'-'
),
'{',
'-'
),
'}',
'-'
),
'[',
'-'
),
']',
'-'
),
'(',
'-'
),
')',
'-'
)
【讨论】:
这可能有用。
此解决方案不涉及创建过程或函数或在替换中长时间使用替换。相反,我们知道所有不涉及特殊字符的 ASCII 字符都位于 ASCII 代码 \x20-\x7E (十六进制表示)中。资源 ASCII From Wikipedia, the free encyclopedia 以下是该区间内的所有字符。
Hex: 20 21 22 23 24 25 26 27 28 29 2A 2B 2C 2D 2E 2F 30 31 32 33 34 35 36 37 38 39 3A 3B 3C 3D 3E 3F 40 41 42 43 44 45 46 47 48 49 4A 4B 4C 4D 4E 4F 50 51 52 53 54 55 56 57 58 59 5A 5B 5C 5D 5E 5F 60 61 62 63 64 65 66 67 68 69 6A 6B 6C 6D 6E 6F 70 71 72 73 74 75 76 77 78 79 7A 7B 7C 7D 7E
Glyph: space ! " # $ % & ' ( ) * + , - . / 0 1 2 3 4 5 6 7 8 9 : ; < = > ? @ A B C D E F G H I J K L M N O P Q R S T U V W X Y Z [ \ ] ↑ ← @ a b c d e f g h i j k l m n o p q r s t u v w x y z { ACK } ESC
所以简单的正则表达式替换就可以了
SELECT REGEXP_REPLACE(columnName, '[^\\x20-\\x7E]', '') from tableName;
PHP自定义查询字符串
$query = "select REGEXP_REPLACE(columnName, '(.*)[(].*[)](.*)', CONCAT('\\\\1', '\\\\2')) `Alias` FROM table_Name";
上面的语句替换了括号之间的内容以及括号中的内容。例如如果该列包含“员工培训 (CMST TOT)”,则上述语句将删除括号及其内容,即“员工培训”。
PS:如果您正在使用存储过程中的准备语句或通过 PHP(创建自定义查询字符串)进行任何 DML(选择、更新...)操作;然后记得转义斜线,即
SET @sql = CONCAT("SELECT REGEXP_REPLACE(columnName, '[^\\\\x20-\\\\x7E]', '') from tableName");
PREPARE stmt FROM @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
上面的 SQL 语句做了一个简单的正则表达式替换(实际上是删除)所有特殊字符;即在 SQL 中,一个 REGEX 模式提到了所有要替换为空的特殊字符。
模式说明
字符组以方括号开头。第一个字符是插入符号,这意味着;否定组中提到的所有字符(即在方括号中)。这只是意味着选择组中所有角色的恭维(除了所选角色之外的其他角色)。
只是总结一下上面的说法
不变:所有字母数字字符、标点符号、算术运算符。
删除所有 Unicode 字符(拉丁字母除外)或特殊字符。
【讨论】:
Adeel 的答案是迄今为止最好和最简单的。
OP 需要更新数据库,这也是我需要的。所以我想我会把它放在这里,为像我这样的下一个可怜的鞋底,而不必重做我做过的事情。
首先仔细检查,选择它并扫描它们以确保在更新之前获得正确的行。
SELECT REGEXP_REPLACE(columnName, '[^\\x20-\\x7E]', '') from tableName;
计数进行安全检查...
SELECT count(*) from tableName WHERE columnName REGEXP '[^\\x20-\\x7E]';
对于某些名称,我必须进行另一次映射,以免失去它们的含义,例如 Ramon 到 Ramn,因为 o 有变音符号、坟墓或抑扬符。所以我用这个来映射...https://theasciicode.com.ar
然后更新此更新是映射更新后的全部内容。把限制数改成上面的计数值……
UPDATE tablename SET columnName = REGEXP_REPLACE(columnName, '[^\\x20-\\x7E]', '') WHERE columnName REGEXP '[^\\x20-\\x7E]' LIMIT 1;
【讨论】: