Home >Database >Mysql Tutorial >How Can I List MySQL Tables Using a SELECT Statement for INSERT Operations?
In MySQL, while the SHOW TABLES command lists tables in a database, it cannot be used within an INSERT statement. To retrieve table names for INSERT using a SELECT statement, a different approach is necessary.
The solution lies in utilizing the information_schema.tables system table. This table provides metadata about tables in the current or a specified database. To list all table names in a database, execute the following query:
SELECT table_name FROM information_schema.tables;
If you wish to filter results based on a specific database, use:
SELECT table_name FROM information_schema.tables WHERE table_schema = 'your_database_name';
To insert these table names into another table, use the following query:
INSERT INTO table_name SELECT table_name FROM information_schema.tables WHERE table_schema = 'your_database_name';
For further details, refer to the MySQL documentation: http://dev.mysql.com/doc/refman/5.0/en/information-schema.html.
The above is the detailed content of How Can I List MySQL Tables Using a SELECT Statement for INSERT Operations?. For more information, please follow other related articles on the PHP Chinese website!