Cú pháp như sau để lấy tên cột của bảng -
select column_name from information_schema.columns where table_schema='yourDatabaseName' and table_name=’yourTableName’;
Đầu tiên chúng ta hãy tạo một bảng -
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) Sau đây là truy vấn để lấy tên cột của bảng. Tại đây, chúng tôi đang tìm nạp tên cột của DemoTable -
mysql> select column_name from information_schema.columns where table_schema='web' and table_name='DemoTable';
Điều này sẽ tạo ra kết quả sau -
+---------------------+ | COLUMN_NAME | +---------------------+ | EmployeeId | | EmployeeFirstName | | EmployeeLastName | | EmployeeAge | | EmployeeCountryName | | IsMarried | | isActive | +---------------------+ 7 rows in set (0.00 sec)