标签:mysql
mysql 查找某值在库中的哪一个表那一个字段(半成品)
DROP PROCEDURE IF EXISTS `test3`;
CREATE DEFINER = `root`@`localhost` PROCEDURE `test3`()
BEGIN
DECLARE tablename varchar(100);
DECLARE columnname varchar(100);
declare b int default 0;
DECLARE cur CURSOR
FOR
select COLUMN_NAME,table_name from information_schema.COLUMNS where table_name in (select table_name from information_schema.tables where table_schema=‘要查找的数据库名‘) limit 0,100; #子查询是根据库名得到该库的所有表名,然后根据表名得到该表的所有列名
DECLARE CONTINUE HANDLER FOR NOT FOUND SET b = 1;
OPEN cur;
FETCH cur into columnname,tablename;
WHILE b<>1 do
#INSERT into tbt(cn,tb) VALUES(columnname,tablename);
set @sqlcmd= concat(‘SELECT ‘,columnname,‘ from 要查找的数据库名.‘,tablename,‘ where ‘,columnname,‘ like "%要查找的值%"‘); #concat()作用是合并字符串
PREPARE stmt1 FROM @sqlcmd; #把@sqlcmd字符串转换为sql语句
EXECUTE stmt1; #执行sql语句
FETCH cur into columnname,tablename;
end while;
close cur;
END;
需要注意的是,因为可能会有太多字段,所以使用了limit ,所以查找到的字段是不齐全,根据情况修改limit的值。
标签:mysql
原文地址:http://8540864.blog.51cto.com/8530864/1713349