mysql - how to save results from DESCRIBE table into table

rahebirizah :

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

Barbaros Özhan :

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.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=298120&siteId=1