mysql - 将表结果显示到临时表 mysql

标签 mysql

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

我们还可以使用 INFORMATION_SCHEMA.TABLES 获取表信息。

但我无法使用 INFORMATION_SCHEMA.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;

糟糕。我没有非常彻底地阅读您的问题,我找到了一种创建以逗号分隔的数据库名称列表的方法,我修改了该方法以返回以逗号分隔的表列表。

以下代码创建了一个临时表,其中包含数据库中的所有表名,它使用 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;

关于mysql - 将表结果显示到临时表 mysql,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18588432/

相关文章:

php - 单个表单上的多个更新按钮

mysql - 在 Rails DB 迁移期间截断表是否有风险?

mysql - 将另一个表中的计数添加到现有查询

mysql - 一对多 MongoDB 与 SQL

Mysql准备做2014年每月分区

php - PHP查询、Web开发

mysql - 如何让MySQL自动更新值?

php - mysql_fetch_array()/mysql_fetch_assoc()/mysql_fetch_row()/mysql_num_rows 等...期望参数 1 是资源

mysql - 减少查询 Yii2

python - MySQL INSERT 在 Python 中失败,但在 MySQL Workbench 中工作正常