简体   繁体   English

搜索表中的所有列

[英]Search all columns in a table

I want a procedure that will search all columns for non keyboard ascii characters (Dec 16 to Dec 31 or DLE to US) and update the column by replacing them with a space ' ' or nothing '' . 我想要一个过程,该过程将在所有列中搜索非键盘ascii字符(从12月16日到12月31日或从DLE到US),并通过用空格' '或none ''替换它们来更新该列。

I have a SELECT statement that is finding the rows I need to update, but I have to manually change all columns myself. 我有一条SELECT语句,该语句查找需要更新的行,但是我必须自己手动更改所有列。

SELECT column_name
FROM table_name
WHERE column_name REGEXP '[[.DLE.]-[.US.]]'

Here's the UPDATE script for modifying the column values 这是用于修改列值的UPDATE脚本

UPDATE table
SET
column = replace(column,char(16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31), '')

I want those two to be fused together to a single function or stored procedure but I don't know how, since I'm just starting to learn MySQL. 我希望将两者融合在一起成为一个函数或存储过程,但是我不知道如何,因为我才刚刚开始学习MySQL。

Disclaimer 放弃

Between using REGEXP and CURSOR s to loop through each table and column, these examples are not going to be lightning fast. 在使用REGEXPCURSOR遍历每个表和列之间,这些示例将不会很快。 The speed will obviously vary depending on your environment and I suggest testing them out on development before production 速度显然会因环境而异,我建议在生产前对它们进行开发测试

One column in one table 一桌一栏

To search a single column on a single table, you basically had the UPDATE as you needed it. 要在单个表上搜索单个列,基本上可以根据需要使用UPDATE

UPDATE t1
SET
column_name = replace(column_name, 
  char(16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31), '')
WHERE column_name REGEXP '[[.DLE.]-[.US.]]'

All columns in one table 一个表中的所有列

To do all columns in a table, you need to identify the table, then loop through the columns using a cursor 要处理表中的所有列,您需要标识表,然后使用游标遍历这些列

DELIMITER $$
CREATE PROCEDURE table_regexp_replace(in_table VARCHAR(128))
BEGIN
  DECLARE done INT DEFAULT FALSE;
  DECLARE search_column VARCHAR(64);

  DECLARE cur1 CURSOR FOR
    SELECT DISTINCT `COLUMN_NAME` FROM `information_schema`.`COLUMNS`
      WHERE `TABLE_NAME` = in_table ORDER BY `ORDINAL_POSITION` ;

  DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;

  OPEN cur1;

  read_loop: LOOP
    -- Process the next column
    FETCH cur1 INTO search_column;

    -- If we're done, stop the loop
    IF done THEN
      LEAVE read_loop;
    END IF;

    -- Replace everything in this column matching the regexp
    SET @new_query := CONCAT ('UPDATE ', in_table, 
        ' SET `', search_column, '` = replace(', search_column, 
          ', char(16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31), \'\')
        WHERE ', search_column, ' REGEXP \'[[.DLE.]-[.US.]]\'') ;

    PREPARE stmt FROM @new_query;
    EXECUTE stmt ;
  END LOOP;

  CLOSE cur1;
END$$

DELIMITER ;

Then usage 然后用法

CALL table_regexp_replace('my_table');

How it works 这个怎么运作

Looks convoluted, it's actually pretty straight forward. 看起来令人费解,实际上非常简单。

  1. We create a procedure with one parameter in_table which is used to specify the table to work with. 我们使用一个参数in_table创建一个过程,该参数用于指定要使用的表。
  2. Setup a cursor that pulls the column names from the information_schema table, in their correct order 设置一个游标,以正确的顺序从information_schema表中提取列名
  3. Loop through each of those columns, executing the manually created UPDATE statement against each one. 遍历所有这些列,对每个列执行手动创建的UPDATE语句。

You'll notice anywhere in the UPDATE query that required quotes, they've had to be escaped using \\ . 您会在UPDATE查询的任何地方看到需要引号的地方,必须使用\\对其进行转义。

\'[[.DLE.]-[.US.]]\'

All columns in all tables 所有表中的所有列

You could then use this procedure in a loop through all tables, using a similar method to above. 然后,您可以使用与上述类似的方法在所有表中循环使用此过程。 Below is how you'd pull all the table names from information_schema : 下面是如何从information_schema提取所有表名:

SELECT DISTINCT TABLE_NAME FROM information_schema . information_schema选择DISTINCT TABLE_NAME。 TABLES WHERE TABLE_SCHEMA = 'your_database_name'; TABLES WHERE TABLE_SCHEMA = 'your_database_name';

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM