Show Tables
命令显示当前数据库中的所有表。
我们也可以INFORMATION_SCHEMA.TABLES
用于表信息。
但我无法使用INFORMATION_SCHEMA.TABLES
.
谁能告诉我
我们如何将“显示表”命令的结果插入表中。
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;
请从表中选择数据并创建临时表,如下所示:
CREATE TEMPORARY TABLE table2 AS (SELECT * FROM table1)