Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> MySQL

แสดงชื่อคอลัมน์ทั้งหมดของตารางใน MySQL

การแสดงรายชื่อฟิลด์ (Columns) ของตาราง

ใช้ INFORMATION_SCHEMA.COLUMNS กรองด้วย table_schema (ฐานข้อมูลปัจจุบัน) และ table_name

รูปแบบคำสั่ง

SELECT column_name AS anyAliasName
FROM information_schema.columns
WHERE table_schema = DATABASE()
  AND table_name = 'yourTableName'\G

ตัวอย่าง: สร้างตารางและแสดงฟิลด์

mysql> CREATE TABLE DemoTable1938 (
    -> StudentId INT,
    -> StudentName VARCHAR(20),
    -> StudentAge INT,
    -> StudentCountryName VARCHAR(20),
    -> StudentMobileNumber BIGINT
    -> );
Query OK, 0 rows affected (0.00 sec)

mysql> SELECT column_name AS ALL_FIELDS
    -> FROM information_schema.columns
    -> WHERE table_schema = DATABASE()
    -> AND table_name = 'DemoTable1938'\G

ผลลัพธ์:

*************************** 1. row ***************************
ALL_FIELDS: StudentId
*************************** 2. row ***************************
ALL_FIELDS: StudentName
*************************** 3. row ***************************
ALL_FIELDS: StudentAge
*************************** 4. row ***************************
ALL_FIELDS: StudentCountryName
*************************** 5. row ***************************
ALL_FIELDS: StudentMobileNumber
5 rows in set (0.00 sec)

หมายเหตุ: ตัวเลือก \G ใช้แสดงผลแนวตั้ง อ่านง่ายกว่าตาราง