Select data from “show tables” MySQL query

后端 未结 13 1669
难免孤独
难免孤独 2020-12-01 04:15

Is it possible to select from show tables in MySQL?

SELECT * FROM (SHOW TABLES) AS `my_tables`

Something along these lines, th

相关标签:
13条回答
  • 2020-12-01 05:00

    You can create a stored procedure and put the table names in a cursor, then loop through your table names to show the data.

    Getting started with stored procedure: http://www.mysqltutorial.org/getting-started-with-mysql-stored-procedures.aspx

    Creating a cursor: http://www.mysqltutorial.org/mysql-cursor/

    For example,

    CREATE PROCEDURE `ShowFromTables`()
    BEGIN
    
    DECLARE v_finished INTEGER DEFAULT 0;
    DECLARE c_table varchar(100) DEFAULT "";
    
    DECLARE table_cursor CURSOR FOR 
    SELECT table_name FROM information_schema.tables WHERE table_name like 'wp_1%';
    
    DECLARE CONTINUE HANDLER 
        FOR NOT FOUND SET v_finished = 1;
    
    OPEN table_cursor;
    
    get_data: LOOP
    
    FETCH table_cursor INTO c_table;
    
    IF v_finished = 1 THEN 
    LEAVE get_data;
    END IF;
    
    SET @s=CONCAT("SELECT * FROM ",c_table,";");
    
    PREPARE stmt FROM @s;
    EXECUTE stmt;
    DEALLOCATE PREPARE stmt;
    
    END LOOP get_data;
    
    CLOSE table_cursor;
    
    END
    

    Then call the stored procedure:

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