简体   繁体   English

将表结果显示为临时表mysql

[英]show tables result into a temp table mysql

Show Tables command shows all the tables presented in current database. Show Tables命令显示当前数据库中显示的所有表。

We can also use INFORMATION_SCHEMA.TABLES for table information. 我们还可以使用INFORMATION_SCHEMA.TABLES来获取表信息。

But i'm not in the position to use INFORMATION_SCHEMA.TABLES . 但我不能使用INFORMATION_SCHEMA.TABLES

Can any one tell me 任何人都可以告诉我

How can we insert result of 'Show Tables' command into a table. 我们如何将'Show Tables'命令的结果插入表中。

I needed to do the same thing. 我需要做同样的事情。 Here are several examples. 以下是几个例子。 I also needed to create tables containing stored procedures and functions, so at the end of the examples I show how to create tables containing procedures and functions. 我还需要创建包含存储过程和函数的表,因此在示例的最后我将展示如何创建包含过程和函数的表。

/*______________________________________
    Quick Version only showing tables.
    No where clause, No table_schema
. . . . . . . . . . . . . . . . . . .*/
DROP TABLE IF EXISTS   tmp_tables;
CREATE TEMPORARY TABLE tmp_tables
SELECT
    *
FROM
    information_schema.tables;

SELECT * FROM tmp_tables;


/*____________________________________________________
    Quick Version with a table name filter
    '%' is a wild card so in this example the results
    may look somthing like:

    TABLE_NAME
    ---------------------
    tbl_employee
    tbl_albumns
. . . . . . . . . . . . . . . . . . . . . . . . . . */
DROP TABLE IF EXISTS   tmp_tables;
CREATE TEMPORARY TABLE tmp_tables
SELECT
    *
FROM
    information_schema.tables
WHERE
    table_name like 'tbl_%';

SELECT * FROM tmp_tables;


/*______________________________________
    This version shows all the column names
    in the select seciton.
    Use with or without the where clause
. . . . . . . . . . . . . . . . . . .*/
DROP TABLE IF EXISTS   tmp_tables;
CREATE TEMPORARY TABLE tmp_tables
SELECT
    TABLE_CATALOG,
    TABLE_SCHEMA,
    TABLE_NAME,
    TABLE_TYPE,
    ENGINE,
    VERSION,
    ROW_FORMAT,
    TABLE_ROWS,
    AVG_ROW_LENGTH,
    DATA_LENGTH,
    MAX_DATA_LENGTH,
    INDEX_LENGTH,
    DATA_FREE,
    AUTO_INCREMENT,
    CREATE_TIME,
    UPDATE_TIME,
    CHECK_TIME,
    TABLE_COLLATION,
    CHECKSUM,
    CREATE_OPTIONS,
    TABLE_COMMENT
FROM
    information_schema.tables
WHERE
    table_schema = DATABASE() # This looks in the current database (schema)
    AND
    table_name like 'tbl_%';

SELECT * FROM tmp_tables;


/*______________________________________
    Quick Version only showing tables.
    Use with or without the where clause
. . . . . . . . . . . . . . . . . . .*/
DROP TABLE IF EXISTS   tmp_tables;
CREATE TEMPORARY TABLE tmp_tables
SELECT
    TABLE_NAME
FROM
    information_schema.tables
WHERE
    table_schema = 'database_name_quoted' #This looks in a specfic database (schema)
    AND
    table_name like '%_tbl_%';

SELECT * FROM tmp_tables;


#==================================
#SOME EXTRA STUFF...
#----------------------------------
# Again these can be used with out
# the WHERE clause or specfic
# elements in the WHERE clause...

# CREATE A TABLE OF PROCEDURES...
DROP TABLE IF EXISTS   tmp_tables;
CREATE TEMPORARY TABLE tmp_tables
SELECT
    *
FROM
    information_schema.routines
WHERE
    ROUTINE_SCHEMA = database()
    and
    ROUTINE_TYPE = 'PROCEDURE'
    AND
    ROUTINE_NAME like 'proc_%';

SELECT * FROM tmp_tables;

# CREATE A TABLE OF FUNCTIONS...
DROP TABLE IF EXISTS   tmp_tables;
CREATE TEMPORARY TABLE tmp_tables
SELECT
    *
FROM
    information_schema.routines
WHERE
    ROUTINE_SCHEMA = database()
    and
    ROUTINE_TYPE = 'FUNCTION'
    AND
    ROUTINE_NAME like 'func_%';

SELECT * FROM tmp_tables;

OOPS. OOPS。 I didn't read you question very thoroughly, I found a way to create a comma delimited list of database names, I modified the method to return a comma delimited list of tables. 我没有彻底读到你的问题,我找到了一种创建逗号分隔的数据库名列表的方法,我修改了方法以返回逗号分隔的表列表。

The following code creates a temporary table that holds all the table names in a database, it does not use information_schema. 以下代码创建一个临时表,其中包含数据库中的所有表名,但它不使用information_schema。 I was able to get a comma delimited list of table names using information from: Shlomi Noach 我能够使用以下信息获得以逗号分隔的表名列表: Shlomi Noach

/*
    This comes from: http://code.openark.org/blog/mysql/reading-results-of-show-statements-on-server-side

    This produces a comma delimited string of tables
    in 'databasename'

    So if your database name is: my_database
    the column name will be: `Tables_in_my_database`

SET @tablename := '';
SHOW TABLES WHERE (@tablename := concat(@tablename, `Tables_in_databasename`, ',')) IS NULL;
SELECT @tablename;

*/


DELIMITER ;;
DROP PROCEDURE IF EXISTS TableList; ;;
CREATE PROCEDURE TableList(IN DatabaseName TEXT, OUT TableList TEXT)
begin
    DEclare ColumnName text;

    SET ColumnName = CONCAT('`Tables_in_', DatabaseName, '`');
    SET @holder = '';

    SET @statement =
    CONCAT
    (
        'SHOW TABLES WHERE (@holder := CONCAT(@holder, ',
        columnname,
        ', ', '\'', ',', '\'', '));'  
    );

    PREPARE statement FROM @statement;
    EXECUTE statement;
    DEALLOCATE PREPARE statement;

    SET TableList = @holder;

    #Create the temporary table that will hold the table names...
    DROP TABLE IF EXISTS   tmp_table_list;
    CREATE TEMPORARY TABLE tmp_table_list (TableName text);

    #Loop through our table list...
    ListLoop: LOOP
        #Check if we are done with the list...
        IF LOCATE(',', @Holder) = 0 THEN
            LEAVE ListLoop;
        END IF;

        #Get the first table name from the list...
        SET @TableName = LEFT(@Holder, (LOCATE(',', @Holder) - 1));

        #Insert our table name into our temporary table...
        INSERT INTO tmp_table_list (TableName) VALUES (@TableName);

        #Remove our table name and the first comma from the table name list...
        SET @Holder = RIGHT(@Holder, LENGTH(@Holder) - (LENGTH(@TableName) + 1));
    END LOOP;

    #We don't have to select and drop our table here
    #But we will for this example...
    SELECT * FROM tmp_table_list;
    DROP TABLE IF EXISTS tmp_table_list;
end;
;;

DELIMITER ;

#SET YOUR DATABASENAME HERE
#                       |
#                       V
CALL TableList('databasename', @TableList);

#SELECT * FROM tmp_table_list;

#SELECT @TableList;

请从表中选择数据并创建临时表,如下所示:

CREATE TEMPORARY TABLE table2 AS (SELECT * FROM table1)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM