Answers:
$ mysqldump -pPASSWORD database --extended=FALSE | grep pattern | less -S
更多:http : //winashwin.wordpress.com/2012/08/28/mysql-search/
早在2012年7月,我写了这篇文章
它使用表information_schema.columns提取每个CHAR,VARCHAR和TEXT字段并执行文本替换。
请查看我的旧链接,并使用其范例进行搜索。
例如,这将为每个表中的每个文本列创建一个单独的SELECT。
SELECT
CONCAT('SELECT ',QUOTE(db),',',QUOTE(tb),',',QUOTE(col),',COUNT(1) FieldHasIt
FROM ',db,'.',tb,' WHERE \`',col,'\`=''',SearchString,''';') SearchSQL
FROM
(
SELECT table_schema db,table_name tb,column_name col FROM information_schema.columns
WHERE table_schema = 'mydb' AND
(column_type LIKE 'char(%' OR column_type LIKE 'varchar(%' OR column_type LIKE '%text')
) A,(SELECT 'Hello' SearchString) B;
使用它创建一个Giant SQL文本文件。然后,执行该Giant SQL脚本:
SQL="SELECT CONCAT('SELECT ',QUOTE(db),',',QUOTE(tb),',',"
SQL="${SQL} QUOTE(col),',COUNT(1) FieldHasIt FROM ',db,'.',tb,'"
SQL="${SQL} WHERE \`',col,'\`=''',SearchString,''';') SearchSQL FROM"
SQL="${SQL} (SELECT table_schema db,table_name tb,column_name col FROM"
SQL="${SQL} information_schema.columns WHERE table_schema='store_qa'"
SQL="${SQL} AND (column_type LIKE 'char(%' OR column_type LIKE 'varchar(%'"
SQL="${SQL} OR column_type LIKE '%text')) A,(SELECT 'Hello' SearchString) B;"
mysql -uroot -p... -ANe"${SQL}" > MegaSearch.sql
mysql -uroot -p... -AN < MegaSearch.sql > MegaSearchResults.txt
RESULTS_FOUND=`grep -c "1$" < MegaSearchResults.txt`
echo ${RESULTS_FOUND}
if [ ${RESULTS_FOUND} -gt 0 ] ; then grep "1$" < MegaSearchResults.txt ; fi
输出告诉您数据出现的数据库,表和列。
试试看 !!!。
编辑:
CONCAT('SELECT ',QUOTE(db),',',QUOTE(tb),',',QUOTE(col),',COUNT(1) FieldHasIt
FROM ',db,'.',tb,' WHERE \`',col,'\`=''',SearchString,''';')
应该:对于名称中带有空格的表,添加下面显示的两个反勾号。
db,'.`',tb,'`',' WHERE
通过构建RolandoMySQLDBA的答案并使用PREPARE
和EXECUTE
运行查询,可以一次返回所有表中与您要查找的所有值匹配的所有列。
-- Your values
SET @table_schema = 'your_table_name';
SET @condition = "LIKE '%your string to search%'";
SET @column_types_regexp = '^((var)?char|(var)?binary|blob|text|enum|set)\\(';
-- Reset @sql_query in case it was used previously
SET @sql_query = '';
-- Build query for each table and merge with previous queries with UNION
SELECT
-- Use `DISTINCT IF(QUERYBUILDING, NULL, NULL)`
-- to only select a single null value
-- instead of selecting the query over and over again as it's built
DISTINCT IF(@sql_query := CONCAT(
IF(LENGTH(@sql_query), CONCAT(@sql_query, " UNION "), ""),
'SELECT ',
QUOTE(CONCAT('`', `table_name`, '`.`', `column_name`, '`')), ' AS `column`, ',
'COUNT(*) AS `occurrences` ',
'FROM `', `table_schema`, '`.`', `table_name`, '` ',
'WHERE `', `column_name`, '` ', @condition
), NULL, NULL) `query`
FROM (
SELECT
`table_schema`,
`table_name`,
`column_name`
FROM `information_schema`.`columns`
WHERE `table_schema` = @table_schema
AND `column_type` REGEXP @column_types_regexp
) `results`;
select @sql_query;
-- Only return results with at least one occurrence
SET @sql_query = CONCAT("SELECT * FROM (", @sql_query, ") `results` WHERE `occurrences` > 0");
-- Run built query
PREPARE statement FROM @sql_query;
EXECUTE statement;
DEALLOCATE PREPARE statement;
如果可以使用bash-这是一个脚本:它需要在数据库上具有用户dbread和pass dbread的权限。
#!/bin/bash
IFS='
'
DBUSER=dbread
DBPASS=dbread
echo -n "Which database do you want to search in (press 0 to see all databases): "
read DB
echo -n "Which string do you want to search: "
read SEARCHSTRING
for i in `mysql $DB -u$DBUSER -p$DBPASS -e "show tables" | grep -v \`mysql $DB -u$DBUSER -p$DBPASS -e "show tables" | head -1\``
do
for k in `mysql $DB -u$DBUSER -p$DBPASS -e "desc $i" | grep -v \`mysql $DB -u$DBUSER -p$DBPASS -e "desc $i" | head -1\` | grep -v int | awk '{print $1}'`
do
if [ `mysql $DB -u$DBUSER -p$DBPASS -e "Select * from $i where $k='$SEARCHSTRING'" | wc -l` -gt 1 ]
then
echo " Your searchstring was found in table $i, column $k"
fi
done
done
如果有人想要解释:http : //infofreund.de/?p=1670
我专注于针对您的问题的简单模板解决方案,该问题在plsql for Oracle中实现数据库实现,我希望在MySql上对其进行自定义,成为简单的Google搜索。您需要做的是研究MySql上的信息架构并替换一些表名和列。
在脚本中:
ALL_TAB_COLUMNS
是一个包含所有表列的信息架构表
'VARCHAR2','NVARCHAR2','NCHAR','CHAR'指的是Oracle中的字符串列类型,您必须将它们替换为与MySql
%hello%等效的类型名称是搜索关键字,用任何字符串替换它。
结果将是一些或许多sql脚本(基于表的列类型),运行它们将得出答案。
另外,性能和时间消耗也将是另一回事。
我希望这会有用
SELECT
'SELECT ' || ALL_TAB_COLUMNS.COLUMN_NAME || ' AS RESULT_FOUND, '
|| '''' || ALL_TAB_COLUMNS.TABLE_NAME ||''''|| ' AS TABLE_NAME, '
|| '''' || ALL_TAB_COLUMNS.COLUMN_NAME ||''''|| ' AS COLUMN_NAME '
|| 'FROM ' || ALL_TAB_COLUMNS.OWNER ||'.'||ALL_TAB_COLUMNS.TABLE_NAME
|| ' WHERE ' || ALL_TAB_COLUMNS.COLUMN_NAME || ' LIKE ''%hello%''' || ';'
FROM ALL_TAB_COLUMNS
WHERE
ALL_TAB_COLUMNS.OWNER = 'SCOTT'
AND
ALL_TAB_COLUMNS.DATA_TYPE IN ('VARCHAR2','NVARCHAR2','NCHAR','CHAR')
使用此技巧与一行代码
SELECT * FROM `table_name1`,`table_name2`
WHERE '$data' IN (`column_name1`,`column_name2`,`column_name3`,`column_name4`);
column_name:键入要在其中搜索的所有列名称。
table_name:键入您要在其中搜索的所有表名。