簡體   English   中英

將表結果顯示為臨時表mysql

[英]show tables result into a temp table mysql

Show Tables命令顯示當前數據庫中顯示的所有表。

我們還可以使用INFORMATION_SCHEMA.TABLES來獲取表信息。

但我不能使用INFORMATION_SCHEMA.TABLES

任何人都可以告訴我

我們如何將'Show Tables'命令的結果插入表中。

我需要做同樣的事情。 以下是幾個例子。 我還需要創建包含存儲過程和函數的表,因此在示例的最后我將展示如何創建包含過程和函數的表。

/*______________________________________
    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;

請從表中選擇數據並創建臨時表,如下所示:

CREATE TEMPORARY TABLE table2 AS (SELECT * FROM table1)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM