Search text in fields in every table of a MySQL database

前端 未结 24 1568
梦谈多话
梦谈多话 2020-11-22 06:23

I want to search in all fields from all tables of a MySQL database a given string, possibly using syntax as:

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


        
相关标签:
24条回答
  • 2020-11-22 06:32

    This is the simple way that's i know. Select your DB in PHPMyAdmin, and go to the "Search" tab and write what you want to find and where you will searching for. Select all tables if you will search the words from all tables. Then "GO" and look the result.

    0 讨论(0)
  • 2020-11-22 06:33

    Using MySQL Workbench it's easy to select several tables and run a search for text in all those tables of the DB ;-)

    0 讨论(0)
  • 2020-11-22 06:33

    I have done this using HeidiSQL. It's not easy to find but by pressing Ctrl+Shift+F it displays the "table tools" dialogue. Then select what you want to search (Full database to single table) and enter the "Text to find" value and click "Find". I found it surprisingly fast (870MiB db in less than a minute)

    0 讨论(0)
  • 2020-11-22 06:36

    In case 23 answers is not enough, here are 2 more... Depending on database structure and content, you may find one them to actually be a quick and simple solution.

    For fans of shell one-liners, here is a long one (actually on 2 lines to use variables):

    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
    

    Or on multiple lines to make it more readable:

    $cmd -s -e 'SHOW TABLES' \
    | while read table; do
        echo "=== $table ===";
        $cmd -B -s -e "SELECT * FROM $table" \
        | grep 'Your_Search';
      done
    
    • -s (--silent) is to suppress the column name headers

    • -B (--batch) escapes special characters like newlines, so we get the whole record when using grep

    And for Perl fans, this will let you use regular expressions:

    # 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
    

    Which in a "real" Perl script could be something like this:

    #!/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;
    
    0 讨论(0)
  • 2020-11-22 06:37

    You could use

    SHOW TABLES;
    

    Then get the columns in those tables (in a loop) with

    SHOW COLUMNS FROM table;
    

    and then with that info create many many queries which you can also UNION if you need.

    But this is extremely heavy on the database. Specially if you are doing a LIKE search.

    0 讨论(0)
  • 2020-11-22 06:37

    i got this to work. you just need to change the variables

    $query ="SELECT `column_name` FROM `information_schema`.`columns` WHERE `table_schema`='" . $_SESSION['db'] . "' AND `table_name`='" . $table . "' ";
    $stmt = $dbh->prepare($query);
    $stmt->execute(); 
    $columns = $stmt->fetchAll(PDO::FETCH_ASSOC);       
    
    $query="SELECT name FROM `" . $database . "`.`" . $table . "` WHERE ( ";
    foreach ( $columns as $column ) {
        $query .=" CONVERT( `" . $column['column_name'] . "` USING utf8 ) LIKE '%" . $search . "%' OR ";
    }
    $query = substr($query, 0, -3);
    $query .= ")";
    
    echo $query . "<br>";
    $stmt=$dbh->prepare($query);
    $stmt->execute();
    $results = $stmt->fetchAll(PDO::FETCH_ASSOC);
    echo "<pre>";
    print_r ($results );
    echo "</pre>";
    
    0 讨论(0)
提交回复
热议问题