简体   繁体   English

mysql按字母顺序获取表列名

[英]mysql get table column names in alphabetical order

Is it possible to query a MySQL database to get the column names of a table in alphabetical order? 是否可以查询MySQL数据库以按字母顺序获取表的列名? I know that 我知道

SHOW COLUMNS `table_name`;

or 要么

DESCRIBE `table_name`;

will give me a list of the columns in a table (along with other info), but is it possible to alter the query in order to get the columns sorted alphabetically. 将为我提供表中列的列表(以及其他信息),但是可以更改查询以便按字母顺序排列列。 Adding ORDER BY 'Field' didn't work, it gave a syntax error. 添加ORDER BY'Field'不起作用,它给出了语法错误。

The ANSI INFORMATION_SCHEMA tables (in this case, INFORMATION_SCHEMA.COLUMNS) provide more flexibility in MySQL: ANSI INFORMATION_SCHEMA表(在本例中为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

Every field was listed twice until I used group by column name 每个字段都列出两次,直到我group by column name使用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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM