如何从批量数据中的mysql列中删除所有非数字字符

时间:2013-09-06 14:44:45

标签: mysql

我想从列中删除所有非数字字符。我的数据库中有批量数据。

目前我正在使用以下链接中描述的方法:

http://venerableagents.wordpress.com/2011/01/29/mysql-numeric-functions/

问题是它需要花费太多时间进行处理。

对于100万行电流逻辑需要1小时来处理数据。

请帮帮我..

谢谢你, Ronak

2 个答案:

答案 0 :(得分:1)

我假设您正在做类似的事情:

update myTable set foo = NumericOnly(foo);

我不知道你能做得多好。

但有一点可能会有所帮助。在那个NumericOnly函数中,他们正在做额外的工作。我将删除SET idx = LENGTH(val)+1;行,因为所有这一切都将重新开始检查字符串的结尾(我们已经检查过的部分)。将检查具有5个前导非数字的字符串,完整,5次。

删除该行将留下:

DROP FUNCTION IF EXISTS NumericOnly;

CREATE FUNCTION NumericOnly (val VARCHAR(255)) 
 RETURNS VARCHAR(255)
BEGIN
 DECLARE idx INT DEFAULT 0;
 IF ISNULL(val) THEN RETURN NULL; END IF;

 IF LENGTH(val) = 0 THEN RETURN ""; END IF;

 SET idx = LENGTH(val);
  WHILE idx > 0 DO
    IF IsNumeric(SUBSTRING(val,idx,1)) = 0 THEN
     SET val = REPLACE(val,SUBSTRING(val,idx,1),"");
    END IF;
    SET idx = idx - 1;
  END WHILE;
  RETURN val;
 END;

答案 1 :(得分:1)

这是对事物的另一种旋转......

DEMO: http://sqlfiddle.com/#!2/0c96e/21

首先,创建一个数字表

CREATE TABLE numbers (
   number int NOT NULL PRIMARY KEY
);

INSERT INTO numbers (number)
SELECT n0 + n1 + n2 + n3 + n4 + n5
FROM   (SELECT 0 AS n0 UNION SELECT 1 UNION SELECT 2 UNION SELECT 3) AS z0
 CROSS
  JOIN (SELECT 0 AS n1 UNION SELECT 4 UNION SELECT 8 UNION SELECT 12) AS z1
 CROSS
  JOIN (SELECT 0 AS n2 UNION SELECT 16 UNION SELECT 32 UNION SELECT 48) AS z2
 CROSS
  JOIN (SELECT 0 AS n3 UNION SELECT 64 UNION SELECT 128 UNION SELECT 192) AS z3
 CROSS
  JOIN (SELECT 0 AS n4 UNION SELECT 256 UNION SELECT 512 UNION SELECT 768) AS z4
 CROSS
  JOIN (SELECT 0 AS n5 UNION SELECT 1024 UNION SELECT 2048 UNION SELECT 3072) AS z5
ORDER
    BY 1;

以下是

的一些示例数据
CREATE TABLE your_table (
   foo varchar(50)
);

INSERT INTO your_table (foo)
  VALUES ('124nhasfonasf13')
       , ('NONE')
       , ('r937')
       , ('o9o9')
       , ('n444n4n455n')
       , ('blah');

然后这是一个查询,只给你数字。应该更高效,因为它是基于SET而不是迭代,就像您的函数示例...

SELECT foo
     , Group_Concat(c ORDER BY position SEPARATOR '')
FROM   (
        SELECT vals.foo
             , numbers.number As position
             , SubString(vals.foo, numbers.number, 1) As c
        FROM   (
                SELECT foo
                     , Length(foo) As lngth
                FROM   your_table
                WHERE  foo REGEXP '[0-9]'
               ) As vals
         INNER
          JOIN numbers
            ON numbers.number BETWEEN 1 AND vals.lngth
       ) As x
WHERE  c REGEXP '[0-9]'
GROUP
    BY foo