如何浏览数据库中的所有表用户ID - X.过程

时间:2012-07-26 08:23:27

标签: mysql search procedure

如何查看数据库中的所有表格,因为我想找到特定数量的UserId,其中X - 它是一个数字UserId示例X = 2所以我必须找到UserId = 2

我使用MySQL,我想要创建程序

我的数据库架构:

Something ( ... ) --> without UserId
Something1 (... , UserId,...)
Something2 (... , UserId,...)
Something3 (... , UserId,...)
Cat
Cat1
Cat2
Cat3
Cat4
etc....

没有数字的表格可能我们称PARENT和表格为数字示例(Cat1,Cat2 ... Cat3 ..)我们称之为CHILDREN>

当我们在一个表中获得20000条记录时,动态创建了CHILDREN

我想要调用过程示例:CALL SearchUser(X)其中X - UserId的数量

如果有人有剧本,我会很感激,或者如果有人帮助我

1 个答案:

答案 0 :(得分:0)

不确定这是否有效。请试一试。 这里的数据库名称是硬编码WHERE TABLE_SCHEMA IN ('test')

## Procedure for search in all fields of all databases
DELIMITER $$
#Script to loop through all tables using Information_Schema
DROP PROCEDURE IF EXISTS get_table $$
CREATE PROCEDURE get_table(in_search varchar(50),in_column_name varchar(50))
READS SQL DATA
BEGIN
DECLARE trunc_cmd VARCHAR(50);
DECLARE search_string VARCHAR(250);
DECLARE db,tbl,clmn CHAR(50);
DECLARE done INT DEFAULT 0;
DECLARE COUNTER INT;
DECLARE table_cur CURSOR FOR
SELECT concat('SELECT COUNT(*) INTO @CNT_VALUE FROM `',table_schema,'`.`',table_name,'` WHERE `', in_column_name,'` = "',in_search,'"') ,table_schema,table_name,column_name FROM information_schema.COLUMNS WHERE TABLE_SCHEMA IN ('test') and column_name=in_column_name ;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done=1;
#Truncating table for refill the data for new search.
PREPARE trunc_cmd FROM "TRUNCATE TABLE temp_details;";
EXECUTE trunc_cmd ; 
OPEN table_cur;
table_loop:LOOP
FETCH table_cur INTO search_string,db,tbl,clmn;
#Executing the search
SET @search_string = search_string;
SELECT  search_string;
PREPARE search_string FROM @search_string;
EXECUTE search_string;
SET COUNTER = @CNT_VALUE;
SELECT COUNTER;
IF COUNTER>0 THEN
# Inserting required results from search to table
INSERT INTO temp_details VALUES(db,tbl,clmn);
END IF;
IF done=1 THEN
LEAVE table_loop;
END IF;
END LOOP;
CLOSE table_cur;

END $$
DELIMITER ;

临时表结构:

CREATE TABLE `temp_details` ( 
`t_schema` varchar(45) NOT NULL,
`t_table` varchar(45) NOT NULL,
`t_field` varchar(45) NOT NULL
) ENGINE=MyISAM DEFAULT CHARSET=latin1;

通话程序:

call get_table('2','userID');
select * from temp_details;