mysql按字母顺序获取表列名

时间:2010-09-24 21:25:06

标签: sql mysql sql-order-by

是否可以查询MySQL数据库以按字母顺序获取表的列名?我知道

SHOW COLUMNS `table_name`;

DESCRIBE `table_name`;

将为我提供表中列的列表(以及其他信息),但是可以更改查询以便按字母顺序对列进行排序。添加ORDER BY'Field'不起作用,它给出了语法错误。

3 个答案:

答案 0 :(得分:31)

ANSI INFORMATION_SCHEMA tables (in this case, INFORMATION_SCHEMA.COLUMNS)在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

答案 1 :(得分:3)

在我使用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

答案 2 :(得分:2)

如果您需要更多详细信息,请在下面查询非常方便:

   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;  --