您无需在列表中添加引号。只需使用LOCATE功能
CREATE PROCEDURE test(taskId int)
begin
declare done int default false;
declare ignore_db varchar(1024);
declare cur1 cursor for select schema_name from information_schema.schemata where schema_name not in (ignore_db);
declare continue handler for not found set done = true;
select value into ignore_db from dbw_parameters where upper(name)=upper('ignore db') and task_id = taskID;
select schema_name from information_schema.schemata
where LOCATE(CONCAT(',',schema_name,','),CONCAT(',',ignore_db,',')) > 0;
end;
这是以这种方式使用 LOCATE 函数的原始示例:
mysql> select LOCATE(',db1,',',db1,db2,db3,');
+---------------------------------+
| LOCATE(',db1,',',db1,db2,db3,') |
+---------------------------------+
| 1 |
+---------------------------------+
1 row in set (0.00 sec)
mysql> select LOCATE(',db2,',',db1,db2,db3,');
+---------------------------------+
| LOCATE(',db2,',',db1,db2,db3,') |
+---------------------------------+
| 5 |
+---------------------------------+
1 row in set (0.00 sec)
mysql> select LOCATE(',db3,',',db1,db2,db3,');
+---------------------------------+
| LOCATE(',db3,',',db1,db2,db3,') |
+---------------------------------+
| 9 |
+---------------------------------+
1 row in set (0.00 sec)
mysql> select LOCATE(',db4,',',db1,db2,db3,');
+---------------------------------+
| LOCATE(',db4,',',db1,db2,db3,') |
+---------------------------------+
| 0 |
+---------------------------------+
1 row in set (0.00 sec)
mysql>
顺便说一句,我用附加逗号包围 ignore_db 的原因与数据库名称本身有关
如果您有具有公共前缀的数据库,则可能会出现您不希望出现的重复数据库。例如,如果 ignore_db 是 db1,db2 而您有数据库 db1,db11,db2,db22,db111,那么所有 5 个数据库都会作为结果出现。因此,我在 WHERE 子句中为 ignore_db 和 schema_name 添加了额外的逗号