MySQL中获取表的列名的语法是什么?

语法如下,以获取表的列名:

select column_name from information_schema.columns
where table_schema='yourDatabaseName' and table_name=’yourTableName’;

让我们首先创建一个表-

mysql> create table DemoTable
(
   EmployeeId int,
   EmployeeFirstName varchar(20),
   EmployeeLastName varchar(20),
   EmployeeAge int,
   EmployeeCountryName varchar(40),
   IsMarried tinyint(1),
   isActive ENUM('ACTIVE','INACTIVE')
);

以下是获取表的列名的查询。在这里,我们获取DemoTable的列名-

mysql> select column_name from information_schema.columns
   where table_schema='web' and table_name='DemoTable';

这将产生以下输出-

+---------------------+
| COLUMN_NAME         |
+---------------------+
| EmployeeId          |
| EmployeeFirstName   |
| EmployeeLastName    |
| EmployeeAge         |
| EmployeeCountryName |
| IsMarried           |
| isActive            |
+---------------------+
7 rows in set (0.00 sec)