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') ); Query OK, 0 rows affected (0.65 sec)
以下查询用于获取表中的列名。在这里,我们获取 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)
广告