Search text in fields in every table of a MySQL database

前端 未结 24 1571
梦谈多话
梦谈多话 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:38

    This is the simplest query to retrive all Columns and Tables

    SELECT * FROM information_schema.`COLUMNS` C WHERE TABLE_SCHEMA = 'YOUR_DATABASE'
    

    All the tables or those with specific string in name could be searched via Search tab in phpMyAdmin.

    Have Nice Query... \^.^/

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

    If you have phpMyAdmin installed use its 'Search' feature.

    • Select your DB
    • Be sure you do have a DB selected (i.e. not a table, otherwise you'll get a completely different search dialog)
    • Click 'Search' tab
    • Choose the search term you want
    • Choose the tables to search

    I have used this on up to 250 table/10GB databases (on a fast server) and the response time is nothing short of amazing.

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

    I built on a previous answer and have this, some extra padding just to be able to conveniently join all the output:

    SELECT 
    CONCAT('SELECT ''',A.TABLE_NAME, '-' ,A.COLUMN_NAME,''' FROM ', A.TABLE_SCHEMA, '.', A.TABLE_NAME, 
           ' WHERE ', A.COLUMN_NAME, ' LIKE \'%Value%\' UNION')
    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'
    UNION SELECT 'SELECT '''
    
    -- for exact match use: A.COLUMN_NAME, ' LIKE \'Value\' instead
    

    First you run this, then paste in and run the result (no editing) and it will display all the table names and columns where the value is used.

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

    Although this question is old , here is how you can do it if you are using mysql workbench 6.3. ( Most likely it also works for other versions)

    Right click your schema and "Search table data" , enter your value and hit "Start Search". Thats it.

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

    I modified the PHP answer of Olivier a bit to:

    • print out the results in which the string was found
    • omit tables without results
    • also show output if column names match the search input
    • show total number of results

      function searchAllDB($search){
          global $mysqli;
      
          $out = "";
          $total = 0;
          $sql = "SHOW TABLES";
          $rs = $mysqli->query($sql);
          if($rs->num_rows > 0){
              while($r = $rs->fetch_array()){
                  $table = $r[0];
                  $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."%')";
                          if(strpos($colum,$search))
                          {
                              echo "FIELD NAME: ".$colum."\n";
                          }
                      }
                      $rs2->close();
                  }
                  $sql_search .= implode(" OR ", $sql_search_fields);
                  $rs3 = $mysqli->query($sql_search);
                  if($rs3 && $rs3->num_rows > 0)
                  {
                      $out .= $table.": ".$rs3->num_rows."\n";
                      if($rs3->num_rows > 0){
                          $total += $rs3->num_rows;
                          $out.= print_r($rs3->fetch_all(),1);
                          $rs3->close();
                      }
                  }
              }
              $out .= "\n\nTotal results:".$total;
              $rs->close();
          }
          return $out;
      }
      
    0 讨论(0)
  • 2020-11-22 06:43

    You can peek into the information_schema schema. It has a list of all tables and all fields that are in a table. You can then run queries using the information that you have gotten from this table.

    The tables involved are SCHEMATA, TABLES and COLUMNS. There are foreign keys such that you can build up exactly how the tables are created in a schema.

    0 讨论(0)
提交回复
热议问题