MySql:显示列但排除字段名称以外的所有内容

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5525970/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 19:22:49  来源:igfitidea点击:

MySql: Show columns but exclude everything except the field names

mysql

提问by ChewyChunks

I'd like to pull a table's field names from MySql into python, and I know that

我想将表的字段名称从 MySql 中提取到 python 中,我知道

'show columns from project'

will work. And I've read that you can add 'WHERE ...' to restrict it to just certain fields. But I can't find an example of how to return just the names of the columns, and not Type, Key, Null, Extra information.

将工作。而且我读到您可以添加“WHERE ...”以将其限制为某些字段。但是我找不到如何仅返回列名称而不是 Type、Key、Null、Extra 信息的示例。

What is the matching criteria to pull all field names for columns and none of the other description stuff?

提取列的所有字段名称而没有其他描述内容的匹配标准是什么?

回答by a_horse_with_no_name

SELECT column_name
FROM information_schema.columns
WHERE  table_name = 'your_table'
   AND table_schema = 'database_name'

回答by Omar Asfour

Although it looks more elegant, you don't need awk for this. MySQL's information_schema.columns table has the info you need.

虽然它看起来更优雅,但你不需要为此使用 awk。MySQL 的 information_schema.columns 表包含您需要的信息。

-- DESCRIBE THE HECK OUT OF THE ENTIRE 'table_name' table in the 'database_name' database

-- 描述 'database_name' 数据库中整个 'table_name' 表的问题

SHOW COLUMNS
FROM database_name.table_name ;

-- SHOW JUST THE COLUMN NAMES for 'table_name' table in the 'database_name' database.

-- 仅显示“database_name”数据库中“table_name”表的列名。

SELECT column_name
FROM information_schema.columns
WHERE table_schema = 'database_name'
AND table_name = 'table_name' ;

回答by fsnow55

Pipe the answer to awk:

将答案传送到 awk:

SHOW columns FROM project; | awk '{ print  }'

回答by Mahbub

You can query MySQL's information_schemadatabase directly for the fieldnames:

您可以information_schema直接查询 MySQL 的数据库以获取字段名:

select distinct(COLUMN_NAME) from information_schema.COLUMNS where TABLE_NAME='posts';

回答by Roger Krueger

If your goal is a comma-delimited list (Not very Python literate, but that's mostly what you'd want in PHP and Perl) GROUP_CONCAT is your friend:

如果你的目标是一个逗号分隔的列表(不是很懂 Python,但这主要是你在 PHP 和 Perl 中想要的) GROUP_CONCAT 是你的朋友:

SELECT GROUP_CONCAT(column_name) FROM information_schema.columns WHERE table_name='your-table-name-here' GROUP BY TABLE_NAME ORDER BY ORDINAL_POSITION

If you need them quoted this gives you all EXCEPT the outermost quotes:

如果您需要引用它们,这将为您提供除最外层引号之外的所有内容:

SELECT GROUP_CONCAT(column_name SEPARATOR '","') FROM information_schema.columns WHERE table_name='your-table-name-here' GROUP BY TABLE_NAME ORDER BY ORDINAL_POSITION

回答by Ali Nawaz

SHOW COLUMNS FROM `table_name`

This MySQL query will work best, it will show all fields of a MySQL table.

这个 MySQL 查询效果最好,它将显示 MySQL 表的所有字段。