我试图弄清楚如何在数据库中定位一个url的所有事件。我想搜索所有表和所有字段。但我不知道从哪里开始,甚至不知道是否有可能。


当前回答

这不是一个优雅的解决方案,但可以通过嵌套循环结构来实现

// select tables from database and store in an array

// loop through the array
foreach table in database
{
    // select columns in the table and store in an array

    // loop through the array
    foreach column in table
    {
        // select * from table where column = url
    }
}

您可以通过在构建列数组时检查哪些列包含字符串,以及将每个表的所有列合并在一个用逗号分隔的WHERE子句中来加快此过程。

其他回答

如果你可以使用bash -这里有一个脚本: 它需要一个用户dbread,并在数据库上传递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

Scott给出了一个很好的例子,但问题是你为什么要这么做?如果您需要对特定字符串执行查找-替换操作,您还可以尝试对数据库执行mysqldump,在编辑器中执行查找-替换操作,然后重新加载数据库。

也许如果你给出一些你想要达到的目标的背景,其他人可能会提供更好的答案。

本视频的前30秒展示了如何使用Phpmyadmin的全局搜索功能 这很有效。它将在每个表中搜索字符串。

http://www.vodahost.com/vodatalk/phpmyadmin-setup/62422-search-database-phpmyadmin.html

一个简单的解决方案是这样做的:

mysqldump -u myuser --no-create-info --extended-insert=FALSE databasename | grep -i "<search string>"

MikeW提出了一个有趣的解决方案,但正如评论中提到的,它是一个SQL Server解决方案而不是MySQL解决方案。下面是一个MySQL解决方案:

use information_schema;
set @q = 'Boston';
set @t = 'my_db';

select CONCAT('use \'',@q,'\';') as q UNION
select CONCAT('select \'', tbl.`TABLE_NAME`,'\' as TableName, \'', col.`COLUMN_NAME`,'\' as Col, `',col.`COLUMN_NAME`,'` as value  from `' , tbl.`TABLE_NAME`,'` where `' ,
        col.`COLUMN_NAME` , '` like \'%' ,@q,  '%\' UNION') AS q
from `tables` tbl
inner join `columns` col on tbl.`TABLE_NAME` = col.`TABLE_NAME`and col.DATA_TYPE='varchar'
where tbl.TABLE_SCHEMA  =  @t  ;