我想在MySQL数据库的所有表中搜索给定字符串的所有字段,可能使用以下语法:
SELECT * FROM * WHERE * LIKE '%stuff%'
可以这样做吗?
您可以执行SQLDump
数据库(及其数据),然后搜索该文件.
如果您安装了phpMyAdmin,请使用其"搜索"功能.
选择您的数据库
确保你选择了一个数据库(即不是一个表,否则你会得到一个完全不同的搜索对话框)
点击"搜索"标签
选择所需的搜索词
选择要搜索的表格
我在多达250个表/ 10GB数据库(在快速服务器上)上使用它,响应时间简直太惊人了.
您可以查看information_schema
架构.它包含表中所有表和所有字段的列表.然后,您可以使用从此表中获取的信息运行查询.
涉及的表格是SCHEMATA,TABLES和COLUMNS.有外键,您可以准确地建立在模式中创建表的方式.
function searchAllDB($search){ global $mysqli; $out = ""; $sql = "show tables"; $rs = $mysqli->query($sql); if($rs->num_rows > 0){ while($r = $rs->fetch_array()){ $table = $r[0]; $out .= $table.";"; $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."%')"; } $rs2->close(); } $sql_search .= implode(" OR ", $sql_search_fields); $rs3 = $mysqli->query($sql_search); $out .= $rs3->num_rows."\n"; if($rs3->num_rows > 0){ $rs3->close(); } } $rs->close(); } return $out; }
您可以使用此项目:http: //code.google.com/p/anywhereindb
这将搜索所有表中的所有数据.
如果你stored procedures
像瘟疫一样避免,或者mysql_dump
由于权限而无法做到,或者遇到其他各种原因.
我建议采用这样的三步法:
1)此查询将一堆查询构建为结果集.
# ================= # VAR/CHAR SEARCH # ================= # BE ADVISED USE ANY OF THESE WITH CAUTION # DON'T RUN ON YOUR PRODUCTION SERVER # ** USE AN ALTERNATE BACKUP ** SELECT CONCAT('SELECT * FROM ', A.TABLE_SCHEMA, '.', A.TABLE_NAME, ' WHERE ', A.COLUMN_NAME, ' LIKE \'%stuff%\';') FROM INFORMATION_SCHEMA.COLUMNS A WHERE A.TABLE_SCHEMA != 'mysql' AND A.TABLE_SCHEMA != 'innodb' AND A.TABLE_SCHEMA != 'performance_schema' AND A.TABLE_SCHEMA != 'information_schema' AND ( A.DATA_TYPE LIKE '%text%' OR A.DATA_TYPE LIKE '%char%' ) ;
.
# ================= # NUMBER SEARCH # ================= # BE ADVISED USE WITH CAUTION SELECT CONCAT('SELECT * FROM ', A.TABLE_SCHEMA, '.', A.TABLE_NAME, ' WHERE ', A.COLUMN_NAME, ' IN (\'%1234567890%\');') FROM INFORMATION_SCHEMA.COLUMNS A WHERE A.TABLE_SCHEMA != 'mysql' AND A.TABLE_SCHEMA != 'innodb' AND A.TABLE_SCHEMA != 'performance_schema' AND A.TABLE_SCHEMA != 'information_schema' AND A.DATA_TYPE IN ('bigint','int','smallint','tinyint','decimal','double') ;
.
# ================= # BLOB SEARCH # ================= # BE ADVISED THIS IS CAN END HORRIFICALLY IF YOU DONT KNOW WHAT YOU ARE DOING # YOU SHOULD KNOW IF YOU HAVE FULL TEXT INDEX ON OR NOT # MISUSE AND YOU COULD CRASH A LARGE SERVER SELECT CONCAT('SELECT CONVERT(',A.COLUMN_NAME, ' USING utf8) FROM ', A.TABLE_SCHEMA, '.', A.TABLE_NAME, ' WHERE CONVERT(',A.COLUMN_NAME, ' USING utf8) IN (\'%someText%\');') FROM INFORMATION_SCHEMA.COLUMNS A WHERE A.TABLE_SCHEMA != 'mysql' AND A.TABLE_SCHEMA != 'innodb' AND A.TABLE_SCHEMA != 'performance_schema' AND A.TABLE_SCHEMA != 'information_schema' AND A.DATA_TYPE LIKE '%blob%' ;
结果应如下所示:
2)然后你就可以Right Click
使用了Copy Row (tab-separated)
3)将结果粘贴到新的查询窗口中并运行到您的心灵内容.
详细信息:除非您Show Metadata and Internal Schemas
选中了选项,否则我会排除您通常在工作台中看不到的系统架构.
我这样做是为了在需要时提供ANALYZE
整个HOST或DB 的快速方法,或运行OPTIMIZE
语句以支持性能改进.
我相信你可以采取不同的方式来做这件事,但这对我有用:
-- ========================================== DYNAMICALLY FIND TABLES AND CREATE A LIST OF QUERIES IN THE RESULTS TO ANALYZE THEM SELECT CONCAT('ANALYZE TABLE ', TABLE_SCHEMA, '.', TABLE_NAME, ';') FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA = 'dbname'; -- ========================================== DYNAMICALLY FIND TABLES AND CREATE A LIST OF QUERIES IN THE RESULTS TO OPTIMIZE THEM SELECT CONCAT('OPTIMIZE TABLE ', TABLE_SCHEMA, '.', TABLE_NAME, ';') FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA = 'dbname';
在MySQL版本上测试:5.6.23
警告:不要运行如下:
您担心导致表锁(密切关注您的客户端连接)
你不确定自己在做什么.
你试图激怒你的DBA.(你的办公桌里的人可能会很快.)
干杯,周杰伦; - ]
我也做了我自己的mysql爬虫来搜索一些wordpress配置,无法在界面和数据库中找到它,并且数据库转储太沉重且不可读.我必须说我现在离不开它.
它的工作方式与@Olivier相似,但它管理着异国情调的数据库/表名,并且是LIKE-joker的安全.
setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); $tables = $dbh->query("SHOW TABLES"); while (($table = $tables->fetch(PDO::FETCH_NUM)) !== false) { $fields = $dbh->prepare("SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_SCHEMA = ? AND TABLE_NAME = ?"); $fields->execute(array ($database, $table[0])); $ors = array (); while (($field = $fields->fetch(PDO::FETCH_NUM)) !== false) { $ors[] = str_replace("`", "``", $field[0]) . " LIKE REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(:search, '\\\\', '\\\\\\\\'), '%', '\\%'), '_', '\\_'), '*', '%'), '?', '_')"; } $request = 'SELECT * FROM '; $request .= str_replace("`", "``", $table[0]); $request .= ' WHERE '; $request .= implode(' OR ', $ors); $rows = $dbh->prepare($request); $rows->execute(array ('search' => $criteria)); $count = $rows->rowCount(); if ($count == 0) { continue; } $str = "Table '{$table[0]}' contains {$count} rows matching '{$criteria}'."; echo str_repeat('-', strlen($str)), PHP_EOL; echo $str, PHP_EOL; echo str_repeat('-', strlen($str)), PHP_EOL; $counter = 1; while (($row = $rows->fetch(PDO::FETCH_ASSOC)) !== false) { $col = 0; $title = "Row #{$counter}:"; echo $title; foreach ($row as $column => $value) { echo (($col++ > 0) ? str_repeat(' ', strlen($title) + 1) : ' '), $column, ': ', trim(preg_replace('!\s+!', ' ', str_replace(array ("\r", "\t", "\n"), array ("", "", " "), $value))), PHP_EOL; } echo PHP_EOL; $counter++; } }
运行此脚本可能会输出如下内容:
--------------------------------------------------- Table 'customers' contains 1 rows matching '*iemblo'. --------------------------------------------------- Row #1: email_client: my@email.com numero_client_compta: C05135 nom_client: Tiemblo adresse_facturation_1: 151, My Street adresse_facturation_2: ville_facturation: Nantes code_postal_facturation: 44300 pays_facturation: FR numero_tva_client: zone_geographique: UE prenom_client: Alain commentaires: nom_societe: email_facturation: my@email.com
这是检索所有列和表的最简单查询
SELECT * FROM information_schema.`COLUMNS` C WHERE TABLE_SCHEMA = 'YOUR_DATABASE'
可以通过phpMyAdmin中的"搜索"选项卡搜索所有表或名称中具有特定字符串的表.
有好的查询......\^.^ /
这是我的解决方案
DROP PROCEDURE IF EXISTS findAll; CREATE PROCEDURE `findAll`( IN `tableName` VARCHAR( 28 ) , IN `search` TEXT ) BEGIN DECLARE finished INT DEFAULT FALSE ; DECLARE columnName VARCHAR ( 28 ) ; DECLARE stmtFields TEXT ; DECLARE columnNames CURSOR FOR SELECT DISTINCT `COLUMN_NAME` FROM `information_schema`.`COLUMNS` WHERE `TABLE_NAME` = tableName ORDER BY `ORDINAL_POSITION` ; DECLARE CONTINUE HANDLER FOR NOT FOUND SET finished = TRUE; SET stmtFields = '' ; OPEN columnNames ; readColumns: LOOP FETCH columnNames INTO columnName ; IF finished THEN LEAVE readColumns ; END IF; SET stmtFields = CONCAT( stmtFields , IF ( LENGTH( stmtFields ) > 0 , ' OR' , '' ) , ' `', tableName ,'`.`' , columnName , '` REGEXP "' , search , '"' ) ; END LOOP; SET @stmtQuery := CONCAT ( 'SELECT * FROM `' , tableName , '` WHERE ' , stmtFields ) ; PREPARE stmt FROM @stmtQuery ; EXECUTE stmt ; CLOSE columnNames ; END;
使用MySQL Workbench,可以轻松选择多个表并在数据库的所有表中搜索文本;-)
我使用HeidiSQL是一个有用和可靠的工具,专为使用流行的MySQL服务器的Web开发人员.
在HeidiSQL中,你可以按shift + ctrl + f,你可以在所有表格中找到服务器上的文本.这个选项非常有用.