Computer >> Hướng Dẫn Máy Tính >  >> Lập Trình >> MySQL

Cú pháp MySQL lấy tên các cột của một bảng

Cú pháp chuẩn

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

Ví dụ: Lấy tên cột của bảng DemoTable trong database 'web'

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

Kết quả:

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

Lưu ý: Thay 'web' bằng tên database thực tế. Cách này hoạt động trên mọi engine lưu trữ.