mysql数据库搜索字符,关于sql:mySQL查询在数据库中搜索字符串的所有表?

是否有mySQL查询来搜索数据库中的所有表?

如果没有,你可以从mySQL工作台GUI搜索数据库中的所有表吗?

从phpmyadmin开始,您可以使用搜索面板选择要搜索的所有表格。 我发现这个超级有效,因为magento,我正在使用的电子商务包有数百个表,不同的产品细节在不同的表中。

043dbade0e6ff502a5c53f448da3523d.png

在MySQL数据库的每个表的所有字段中搜索可能重复

我建议在DROP PROCEDURE之前添加行SET sql_notes = 0 $$如果EXISTS get_table $$否则它将显示警告PROCEDURE不存在。

如果您使用的是MySQL Workbench,可以通过右键单击要搜索的DB Schema,然后"Search Table Data ..."来完成此操作。

在那里,您可以选择"使用REXEXP搜索"选项,然后像往常一样键入搜索文本。它将提供与您的特定文本匹配的数据库行。

您还需要选中"搜索所有类型的列"框。

如果你想在没有任何编程语言帮助的情况下完全在MySQL中完成它,你可以使用:

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

资料来源:http://forge.mysql.com/tools/tool.php?id = 232

老帖子,但我也在寻找类似的东西,但在任何程度上都没有使用过MySQL函数或程序。 OP正在寻找能够搜索数据库中所有表的东西,但是这个过程似乎搜索所有数据库。 它怎么能仅限于它所在的那个?

它的工作方式稍有不同,因为它使用search_string = query(在LOOP中查询)返回数组。 但是当我评论行时:SELECT search_string; 和SELECT COUNTER; ,然后它按预期运行良好,程序返回正确的内容。 它可能是DB版本,我的是MariaDB 10.1.31。

或者,如果您的数据库不是那么庞大,您可以进行转储并在.sql生成的文件中进行搜索。

在数据库的所有表中搜索字符串是一项复杂的任务。通常,您不需要使用完全所有的表,并且在没有特定布局(具有匹配的表格的树等)的情况下读取结果很复杂

SQL Workbench / J提供GUI和命令行版本来执行此类任务:

更多信息:

http://www.sql-workbench.net/manual/wb-commands.html#command-search-data

http://www.sql-workbench.net/manual/dbexplorer.html#search-table-data

注意:如果配置不正确,使用jdbc驱动程序搜索会占用大量内存。 SQL Workbench / J警告说,虽然在线文档有点过时,文档的来源(doc / xml / db-problems.xml)解释了如何为不同的BBDD修复它:

这里是postgres的摘录:

The PostgreSQL JDBC driver defaults to buffer the results obtained from the database

in memory before returning them to the application. This means that when retrieving

data, &wb-productname; uses (for a short amount of time) twice as much memory as really

needed. This also means that WbExport or WbCopy will

effectively read the entire result into memory before writing it into the output file.

For large exports this us usually not wanted.

This behaviour of the driver can be changed so that the driver uses cursor based retrieval.

To do this, the connection profile must disable the"Autocommit" option, and must define

a default fetch size that is greater than zero. A recommended value is e.g. 10, it might be

that higher numbers give a better performance. The number defined for the fetch size,

defines the number of rows the driver keeps in its internal buffer before requesting more

rows from the backend.

在MySQL Workbench中,您可以使用表数据搜索功能。它可以搜索多个表和/或多个数据库。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值