失眠网,内容丰富有趣,生活中的好帮手!
失眠网 > mysql搜索所有表 mySQL查询来搜索数据库中的所有表以查找字符串?

mysql搜索所有表 mySQL查询来搜索数据库中的所有表以查找字符串?

时间:2023-04-19 13:57:21

相关推荐

mysql搜索所有表 mySQL查询来搜索数据库中的所有表以查找字符串?

Is there a mySQL query to search all tables within a database?

If not can you search all tables within a database from the mySQL workbench GUI?

From phpmyadmin there's a search panel you can use to select all tables to search through. I find this super effective since magento, the ecommerce package I'm working with has hundreds of tables and different product details are in different tables.

解决方案

If you want to do it purely in MySQL, without the help of any programming language, you could use this:

## Table for storing resultant output

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;

## 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))

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 `', column_name,'` REGEXP ''',in_search,''';')

,table_schema,table_name,column_name

FROM information_schema.COLUMNS

WHERE TABLE_SCHEMA NOT IN ('information_schema','test','mysql');

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;

#Finally Show Results

SELECT * FROM temp_details;

END $$

DELIMITER ;

如果觉得《mysql搜索所有表 mySQL查询来搜索数据库中的所有表以查找字符串?》对你有帮助,请点赞、收藏,并留下你的观点哦!

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。