如何從 MySQL 中提取列名和型別?


若要提取列名和型別,請使用 INFORMATION_SCHEMA.COLUMNS −

select concat(column_name,'=',data_type) as anyAliasName from information_schema.columns
where table_schema= yourDatabaseName and table_name= yourTableName;

我們先建立一個表 −

mysql> create table DemoTable1812
     (
     Id int,
     FirstName varchar(20),
     Age int,
     isMarried boolean,
     status ENUM('ACTIVE','INACTIVE')
     );
Query OK, 0 rows affected (0.00 sec)

以下是從 MySQL 中提取列名和型別的查詢

mysql> select concat(column_name,'=',data_type) as COLUMNNAMEANDTYPE from information_schema.columns
     where table_schema= 'web' and table_name= 'DemoTable1812';

這將產生以下輸出 −

+-------------------+
| COLUMNNAMEANDTYPE |
+-------------------+
| Id=int            |
| FirstName=varchar |
| Age=int           |
| isMarried=tinyint |
| status=enum       |
+-------------------+
5 rows in set (0.00 sec)

更新於: 24-Dec-2019

440 次瀏覽

開啟您的 職業生涯

完成課程以獲得認證

開始學習
廣告