mysql get table column names in alphabetical order

后端 未结 3 662
情深已故
情深已故 2020-12-09 03:22

Is it possible to query a MySQL database to get the column names of a table in alphabetical order? I know that

SHOW COLUMNS `table_name`;

相关标签:
3条回答
  • 2020-12-09 03:39

    The ANSI INFORMATION_SCHEMA tables (in this case, INFORMATION_SCHEMA.COLUMNS) provide more flexibility in MySQL:

    SELECT c.column_name
      FROM INFORMATION_SCHEMA.COLUMNS c
     WHERE c.table_name = 'tbl_name'
    -- AND c.table_schema = 'db_name'    
    ORDER BY c.column_name
    
    0 讨论(0)
  • 2020-12-09 03:48

    Every field was listed twice until I used group by column name

     select c.COLUMN_NAME from INFORMATION_SCHEMA.COLUMNS c 
     where c.TABLE_NAME = `'tbl_name'` 
     group by c.column_name 
     order by c.column_name
    
    0 讨论(0)
  • 2020-12-09 03:54

    If you want more details, below query is really handy:

       SELECT COLUMN_NAME  as 'Field',
       COLUMN_TYPE    as 'Type',
       IS_NULLABLE    as 'Null',
       COLUMN_KEY     as 'Key',
       COLUMN_DEFAULT as 'Default',
       EXTRA          as 'Extra'
       from INFORMATION_SCHEMA.COLUMNS
       where TABLE_NAME   = 'my table' and
       TABLE_SCHEMA = 'my database'
       add ordering
       order by Type;  -- 
    
    0 讨论(0)
提交回复
热议问题