mySQL 查询在数据库中的所有表中搜索字符串?

发布于 2024-12-05 23:29:10 字数 285 浏览 1 评论 0原文

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

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

phpmyadmin 中有一个搜索面板,您可以使用它来选择所有表格进行搜索。我发现这非常有效,因为我正在使用的电子商务包 magento 有数百个表,并且不同的产品详细信息位于不同的表中。

在此处输入图像描述

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.

enter image description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

新雨望断虹 2024-12-12 23:29:10

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

Alternatively, if your database is not that huge, you can make a dump and make your search in the .sql generated file.

新一帅帅 2024-12-12 23:29:10

如果您使用MySQL Workbench,则可以通过右键单击要搜索的数据库架构,然后“搜索表数据...”来完成此操作。

您可以在其中选择“使用 REXXP 搜索”选项,然后照常输入搜索文本。它将提供与您的特定文本匹配的数据库行。

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

If you are using MySQL Workbench, you can do this by doing right click on the DB Schema you want to search into, and then "Search Table Data...".

In there you can select the "Search using REXEXP" option, and then type your text of search as usual. It will provide the DB rows matching your specific text.

You will need to check the "Search columns of all types" box as well.

很糊涂小朋友 2024-12-12 23:29:10

如果你想纯粹在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

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 ;

Source: http://forge.mysql.com/tools/tool.php?id=232

独﹏钓一江月 2024-12-12 23:29:10

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

In MySQL Workbench you can use the Table Data Search feature. It can search across multiple tables and/or multiple databases.

递刀给你 2024-12-12 23:29:10

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

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

更多信息:

注意: 如果使用 JDBC 驱动程序进行搜索,则会占用大量内存未正确配置。 SQL Workbench/J 对此发出警告,尽管在线文档有点过时,但文档来源 (doc/xml/db-problems.xml) 解释了如何针对不同的 BBDD 修复它:

这里是 Postgres 的摘录:

PostgreSQL JDBC驱动程序默认缓冲从数据库获取的结果
在将它们返回到应用程序之前先将其存储在内存中。这意味着检索数据时, &wb-productname; (在短时间内)使用实际需要两倍的内存。这也意味着 WbExport 或 WbCopy 将
在将整个结果写入输出文件之前,有效地将整个结果读入内存。
对于大量出口,这通常是不需要的。
可以更改驱动程序的这种行为,以便驱动程序使用基于游标的检索。
为此,连接配置文件必须禁用“自动提交”选项,并且必须定义大于零的默认提取大小。推荐值例如为 10,数字越高可能性能越好。为获取大小定义的数字,
定义驱动程序在请求更多之前在其内部缓冲区中保留的行数
来自后端的行。

Search string in all tables on a database is a complex task. Normally you don't need to use exactly all tables and results are complex to read without a specific layout (tree of tables with matches or the like)

SQL Workbench/J offers a GUI and a command-line version to do such task:

More info:

NOTE: Search with JDBC driver uses a lot of memory if it is not configured properly. SQL Workbench/J warns about that and although online documentation is a bit outdated, the sources of documentation (doc/xml/db-problems.xml) explain how to fix it for different BBDD:

Here an extract for 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 is usually not wanted.
This behavior 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文