我想从一个MySQL数据库的所有表的所有字段搜索一个给定的字符串,可能使用语法为:

SELECT * FROM * WHERE * LIKE '%stuff%'

有可能做这样的事情吗?


当前回答

这是检索所有列和表的最简单的查询

SELECT * FROM information_schema.`COLUMNS` C WHERE TABLE_SCHEMA = 'YOUR_DATABASE'

在phpMyAdmin中,可以通过搜索选项卡搜索所有表或名称中有特定字符串的表。

有良好的查询…\ ^ ^ /

其他回答

MySQL工作台

这里有一些说明。

下载并安装MSQL Workbench。

https://www.mysql.com/products/workbench/

安装时,可能需要安装Visual Studio c++ Redistributable。你可以在这里买到:

https://support.microsoft.com/en-us/help/2977003/the-latest-supported-visual-c-downloads

x64: vc_redist.x64.exe(适用于64位Windows)

打开MySQL Workbench时,必须输入主机名、用户名和密码。

在侧菜单栏上有一个Schemas选项卡,单击Schemas选项卡,然后双击数据库以选择要搜索的数据库。

然后转到菜单数据库-搜索数据,并输入你要搜索的文本,单击开始搜索。

海蒂SQLl

下载并安装HeidiSql https://www.heidisql.com/download.php

输入主机名、用户名和密码。

按Ctrl+Shift+F搜索文本。

我修改了一点Olivier的PHP答案:

print out the results in which the string was found omit tables without results also show output if column names match the search input show total number of results function searchAllDB($search){ global $mysqli; $out = ""; $total = 0; $sql = "SHOW TABLES"; $rs = $mysqli->query($sql); if($rs->num_rows > 0){ while($r = $rs->fetch_array()){ $table = $r[0]; $sql_search = "select * from ".$table." where "; $sql_search_fields = Array(); $sql2 = "SHOW COLUMNS FROM ".$table; $rs2 = $mysqli->query($sql2); if($rs2->num_rows > 0){ while($r2 = $rs2->fetch_array()){ $colum = $r2[0]; $sql_search_fields[] = $colum." like('%".$search."%')"; if(strpos($colum,$search)) { echo "FIELD NAME: ".$colum."\n"; } } $rs2->close(); } $sql_search .= implode(" OR ", $sql_search_fields); $rs3 = $mysqli->query($sql_search); if($rs3 && $rs3->num_rows > 0) { $out .= $table.": ".$rs3->num_rows."\n"; if($rs3->num_rows > 0){ $total += $rs3->num_rows; $out.= print_r($rs3->fetch_all(),1); $rs3->close(); } } } $out .= "\n\nTotal results:".$total; $rs->close(); } return $out; }

这个解决方案 a)只有MySQL,不需要其他语言,并且 b)返回SQL结果,准备处理!

#Search multiple database tables and/or columns
#Version 0.1 - JK 2014-01
#USAGE: 1. set the search term @search, 2. set the scope by adapting the WHERE clause of the `information_schema`.`columns` query
#NOTE: This is a usage example and might be advanced by setting the scope through a variable, putting it all in a function, and so on...

#define the search term here (using rules for the LIKE command, e.g % as a wildcard)
SET @search = '%needle%';

#settings
SET SESSION group_concat_max_len := @@max_allowed_packet;

#ini variable
SET @sql = NULL;

#query for prepared statement
SELECT
    GROUP_CONCAT("SELECT '",`TABLE_NAME`,"' AS `table`, '",`COLUMN_NAME`,"' AS `column`, `",`COLUMN_NAME`,"` AS `value` FROM `",TABLE_NAME,"` WHERE `",COLUMN_NAME,"` LIKE '",@search,"'" SEPARATOR "\nUNION\n") AS col
INTO @sql
FROM `information_schema`.`columns`
WHERE TABLE_NAME IN
(
    SELECT TABLE_NAME FROM `information_schema`.`columns`
    WHERE
        TABLE_SCHEMA IN ("my_database")
        && TABLE_NAME IN ("my_table1", "my_table2") || TABLE_NAME LIKE "my_prefix_%"
);

#prepare and execute the statement
PREPARE stmt FROM @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

我已经使用HeidiSQL做到了这一点。 它不容易找到,但按Ctrl+Shift+F它会显示“表格工具”对话框。然后选择要搜索的内容(全库到单表),输入“要查找的文本”值,点击“查找”。我发现它惊人的快(不到一分钟870MiB db)

如果你安装了phpMyAdmin,使用它的“搜索”功能。

选择您的数据库 确保你有一个DB选择(即不是一个表,否则你会得到一个完全不同的搜索对话框) 点击“搜索”标签 选择您想要的搜索词 选择要搜索的表

我在250个表/10GB的数据库上使用过这个功能(在一个快速的服务器上),响应时间非常惊人。