How to search for something in 100% of MySQL database

1

Is there a MySQL query in which I can fetch all data from all tables in a database at one time?

Equivalent to something like this:

"SELECT todos_os_campos FROM todas_as_tabelas WHERE qualquer_campo = 'isso'"
    
asked by anonymous 30.11.2017 / 12:36

2 answers

1

In this procedure it looks in all the fields that are not numeric certain content, first parameter you pass what you want to search and in the second the name of the database, it will return all the results found. in separate queries, because you can not give a UNION ALL because the tables do not have the same number of columns. Follow the Code:

DROP PROCEDURE IF EXISTS sp_search_data;        

DELIMITER |

CREATE PROCEDURE sp_search_data(p_conteudo TEXT,                                                                        
                                p_nome_banco TEXT)
BEGIN

    DECLARE v_nome_tabela TEXT;
    DECLARE v_nome_coluna TEXT;
    DECLARE v_possui_registro INT;
    DECLARE v_fim INT DEFAULT 0;
    DECLARE cur_tabelas CURSOR FOR SELECT  TABLE_NAME,                                                                              
                                           COLUMN_NAME
                                    FROM information_schema.'COLUMNS'                                                                           
                                   WHERE TABLE_SCHEMA = p_nome_banco
                                     AND NUMERIC_PRECISION IS NULL;

    DECLARE CONTINUE HANDLER FOR NOT FOUND SET v_fim = 1;

    OPEN cur_tabelas;

    REPEAT

        IF NOT v_fim THEN
        FETCH cur_tabelas INTO v_nome_tabela, v_nome_coluna;

        SET @v_possui_registro = 0;
        SET @v_select = CONCAT('SELECT COUNT(*) INTO @v_possui_registro
                                  FROM ',v_nome_tabela,'
                                 WHERE ', v_nome_coluna,' = "',p_conteudo,'"');

        PREPARE stmt_select FROM @v_select;
        EXECUTE stmt_select;
        DEALLOCATE PREPARE stmt_select;

        IF @v_possui_registro > 0 THEN

            SET @v_resultado = CONCAT('SELECT *
                                         FROM ',v_nome_tabela,'
                                        WHERE ', v_nome_coluna,' = "',p_conteudo,'"');
            PREPARE stmt_resultado FROM @v_resultado;
            EXECUTE stmt_resultado;
            DEALLOCATE PREPARE stmt_resultado;
        END IF;

    END IF;
   UNTIL v_fim END REPEAT;
   CLOSE cur_tabelas;


END
|
DELIMITER ;

-- CALL sp_search_data('TESTE', 'banco_teste');

Remember that in this procedure you can make several implementations, such as printing the name of the table and column that was found in the registry, printing the query that was done among other interesting things,     

30.11.2017 / 13:46
0

Well, native SQL does not have this feature, but if you want to SELECT in all tables in a query you can only use UNION to bring the records into a single query. >

SELECT * FROM Tabela_1 WHERE condicao = 1
UNION  
SELECT * FROM Tabela_2 WHERE condicao = 2
UNION
SELECT * FROM Tabela_3 WHERE condicao = 3
UNION
SELECT * FROM Tabela_4 WHERE condicao = 4
UNION
...
// assim por diante com todas as tabelas

I know that this way can be impractical if you have 10,000 tables, but I give you this option as long as it's feasible for you to do so.

    
30.11.2017 / 12:54