MySQL mysql按字母顺序获取表列名
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3791123/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
mysql get table column names in alphabetical order
提问by John Scipione
Is it possible to query a MySQL database to get the column names of a table in alphabetical order? I know that
是否可以查询 MySQL 数据库以按字母顺序获取表的列名?我知道
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' 不起作用,它给出了语法错误。
回答by OMG Ponies
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
回答by Stephen
Every field was listed twice until I used 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
回答by user3805033
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; --