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)
廣告