我想从一个MySQL数据库的所有表的所有字段搜索一个给定的字符串,可能使用语法为:
SELECT * FROM * WHERE * LIKE '%stuff%'
有可能做这样的事情吗?
我想从一个MySQL数据库的所有表的所有字段搜索一个给定的字符串,可能使用语法为:
SELECT * FROM * WHERE * LIKE '%stuff%'
有可能做这样的事情吗?
当前回答
我不知道这是否只在最近的版本中,但右键单击Navigator窗格中的Tables选项会弹出一个名为Search Table Data的选项。这将打开一个搜索框,您可以在其中填写搜索字符串并点击搜索。
您确实需要在左侧窗格中选择要搜索的表。但如果你按住shift键一次选择10个表,MySql可以在几秒钟内处理并返回结果。
对于任何正在寻找更好选择的人!:)
其他回答
这个解决方案 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)
您可以查看information_schema模式。它包含所有表和表中所有字段的列表。然后,您可以使用从该表中获得的信息运行查询。
涉及的表包括SCHEMATA、tables和COLUMNS。有一些外键,这样您就可以在模式中准确地构建表的创建方式。
如果23个答案还不够,这里还有2个……根据数据库结构和内容,您可能会发现其中一个实际上是快速而简单的解决方案。
对于shell一行程序的爱好者,这里有一个很长的程序(实际上只有2行,使用变量):
cmd='mysql -u Username -pYour_Password -D Your_Database' # <-- Adapt this
$cmd -s -e 'SHOW TABLES' | while read table; do echo "=== $table ==="; $cmd -B -s -e "SELECT * FROM $table" | grep 'Your_Search'; done
或多行,使其更具可读性:
$cmd -s -e 'SHOW TABLES' \
| while read table; do
echo "=== $table ===";
$cmd -B -s -e "SELECT * FROM $table" \
| grep 'Your_Search';
done
-s(——silent)用于屏蔽列名标头 -B(——batch)转义像换行符这样的特殊字符,所以我们在使用grep时获得整个记录
对于Perl爱好者来说,这将允许您使用正则表达式:
# perl -MDBI -le '($db,$u,$p)=@ARGV; $dbh=DBI->connect("dbi:mysql:dbname=$db",$u,$p); foreach $table ($dbh->tables()) {print "$table\n"; foreach $r ($dbh->selectall_array("SELECT * FROM $table")) {$_=join("\t", @$r); print $_ if (/Your_Regex/);}}' Your_Database Username Your_Password
在“真正的”Perl脚本中可能是这样的:
#!/usr/bin/perl
use strict;
use open qw(:std :utf8);
use DBI;
my $db_host = 'localhost';
my $db = 'Your_Database';
my $db_user = 'Username';
my $db_pass = 'Your_Password';
my $search = qr/Your_regex_Search/;
# https://metacpan.org/pod/DBD::mysql
my $dbh = DBI->connect( "dbi:mysql:dbname=$db;host=$db_host", $db_user, $db_pass,
{ mysql_enable_utf8mb4 => 1 }
) or die "Can't connect: $DBI::errstr\n";
foreach my $table ( $dbh->tables() ) {
my $sth = $dbh->prepare("SELECT * FROM $table")
or die "Can't prepare: ", $dbh->errstr;
$sth->execute
or die "Can't execute: ", $sth->errstr;
my @results;
while (my @row = $sth->fetchrow()) {
local $_ = join("\t", @row);
if ( /$search/ ) {
push @results, $_;
}
}
$sth->finish;
next unless @results;
print "*** TABLE $table :\n",
join("\n---------------\n", @results),
"\n" . "=" x 20 . "\n";
}
$dbh->disconnect;
即使下面的建议不应该被认为是最终的解决方案,你也可以通过这样做来实现目标:
SET SESSION group_concat_max_len = 1000000;
SET @search = 'Text_To_Search';
DROP table IF EXISTS table1;
CREATE TEMPORARY TABLE table1 AS
(SELECT
CONCAT('SELECT \'',TABLE_NAME,'\' as \'table_name\',\'',COLUMN_NAME,'\' as \'column_name\',CONVERT(count(*),char) as \'matches\' FROM `',
TABLE_NAME,'` where `',COLUMN_NAME,'` like \'%',@search,'%\' UNION ') as 'query'
FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_SCHEMA = 'db_name' limit 1000000);
set @query = (SELECT GROUP_CONCAT(t1.`query` SEPARATOR '') as 'final_query' from table1 t1 limit 1);
set @query = (SELECT SUBSTRING(@query, 1, length(@query) - 7));
PREPARE stmt FROM @query;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
请记住:
选项:group_concat_max_len和limit 1000000并不总是必需的,这将取决于您的服务器/IDE配置。以防我加进去。 执行此命令后,您将得到一个3列响应:[table_name], [column_name], [matches] 列'matches'是给定表/列中出现的次数。 这个查询非常快。
免责声明:它只对个人使用有用,换句话说,请不要在生产系统中使用它,因为考虑到搜索参数与其他字符串连接,它对SQL注入攻击很敏感。 如果你想创建一个prod. ready函数,那么你需要创建一个带有LOOP的存储过程。