首页 > 解决方案 > mysql - how to save results from DESCRIBE table into table

问题描述

I'm trying to implement the following code but doesn't seem to work in mysql:

insert into table2
DESCRIBE table1;

or

insert into table2
SHOW COLUMNS FROM table1;

any help is appreciated.

regards

标签: mysqldatabasemariadbsql-insertmariadb-10.1

解决方案


You can use such a way through use of information_schema.columns :

CREATE TABLE table2(colname varchar(100), coltype varchar(100));

CREATE TABLE table1(col1 varchar(100), col2 int);

INSERT INTO table2
SELECT column_name, data_type 
  FROM information_schema.columns 
 WHERE table_name = 'table1'

Demo

The DDL and DML statements are mixed, which is violation, within your cases.


推荐阅读