将表结果显示为临时表mysql

时间:2013-09-03 09:16:54

标签: mysql

Show Tables命令显示当前数据库中显示的所有表。

我们也可以使用INFORMATION_SCHEMA.TABLES来获取表格信息。

但我不能使用INFORMATION_SCHEMA.TABLES

任何人都可以告诉我

我们如何将'Show Tables'命令的结果插入表中。

2 个答案:

答案 0 :(得分:1)

我需要做同样的事情。以下是几个例子。 我还需要创建包含存储过程和函数的表, 所以在示例的最后我将展示如何创建包含过程和函数的表。

/*______________________________________
    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。我没有仔细阅读你的问题,我找到了一种创建逗号分隔的数据库名列表的方法,我修改了方法以返回以逗号分隔的表列表。

以下代码创建一个临时表,其中包含数据库中的所有表名,使用information_schema。 我能够使用以下信息获得以逗号分隔的表名列表: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;

答案 1 :(得分:0)

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

CREATE TEMPORARY TABLE table2 AS (SELECT * FROM table1)