Search text in fields in every table of a MySQL database

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

    I don't know if this is only in the recent versions, but right clicking on the Tables option in the Navigator pane pops up an option called Search Table Data. This opens up a search box where you fill in the search string and hit search.

    You do need to select the table you want to search in on the left pane. But if you hold down shift and select like 10 tables at a time, MySql can handle that and return results in seconds.

    For anyone that is looking for better options! :)

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

    I used Union to string together queries. Don't know if it's the most efficient way, but it works.

    SELECT * FROM table1 WHERE name LIKE '%Bob%' Union
    SELCET * FROM table2 WHERE name LIKE '%Bob%';
    
    0 讨论(0)
  • 2020-11-22 06:46

    This solution
    a) is only MySQL, no other language needed, and
    b) returns SQL results, ready for processing!

    #Search multiple database tables and/or columns
    #Version 0.1 - JK 2014-01
    #USAGE: 1. set the search term @search, 2. set the scope by adapting the WHERE clause of the `information_schema`.`columns` query
    #NOTE: This is a usage example and might be advanced by setting the scope through a variable, putting it all in a function, and so on...
    
    #define the search term here (using rules for the LIKE command, e.g % as a wildcard)
    SET @search = '%needle%';
    
    #settings
    SET SESSION group_concat_max_len := @@max_allowed_packet;
    
    #ini variable
    SET @sql = NULL;
    
    #query for prepared statement
    SELECT
        GROUP_CONCAT("SELECT '",`TABLE_NAME`,"' AS `table`, '",`COLUMN_NAME`,"' AS `column`, `",`COLUMN_NAME`,"` AS `value` FROM `",TABLE_NAME,"` WHERE `",COLUMN_NAME,"` LIKE '",@search,"'" SEPARATOR "\nUNION\n") AS col
    INTO @sql
    FROM `information_schema`.`columns`
    WHERE TABLE_NAME IN
    (
        SELECT TABLE_NAME FROM `information_schema`.`columns`
        WHERE
            TABLE_SCHEMA IN ("my_database")
            && TABLE_NAME IN ("my_table1", "my_table2") || TABLE_NAME LIKE "my_prefix_%"
    );
    
    #prepare and execute the statement
    PREPARE stmt FROM @sql;
    EXECUTE stmt;
    DEALLOCATE PREPARE stmt;
    
    0 讨论(0)
  • 2020-11-22 06:49

    Here is my solution for this

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

    I am use HeidiSQL is a useful and reliable tool designed for web developers using the popular MySQL server.

    In HeidiSQL you can push shift + ctrl + f and you can find text on the server in all tables. This option is very usefully.

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

    PHP function:

    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()){
                        $column = $r2[0];
                        $sql_search_fields[] = $column." 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;
    }
    
    0 讨论(0)
提交回复
热议问题