如何获取所有列' MySQL 中所有表的名称?
有没有一种快速方法可以从 MySQL 中的所有表中获取所有列名,而不必列出所有表?
Is there a fast way of getting all column names from all tables in MySQL
, without having to list all the tables?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(11)
要列出 MySQL 中表的所有字段:
To list all the fields from a table in MySQL:
显示表名中的列
Show columns from tablename
由于我没有足够的代表来发表评论,因此(在我看来)对 nick Rulez 的出色答案做了一个小小的改进:将
WHERE table_schema = 'your_db'
替换为WHERE table_schema = DATABASE()
。Since I don't have enough rep to comment, here's a minor improvement (in my view) over nick rulez's excellent answer: replacing
WHERE table_schema = 'your_db'
withWHERE table_schema = DATABASE()
.如果它对其他人有用,这将为您提供每个表中以逗号分隔的列列表:
注意:当使用具有大量列和/或长字段名称的表时,请注意group_concat_max_len 限制,这可以导致数据被截断。
On the offchance that it's useful to anyone else, this will give you a comma-delimited list of the columns in each table:
Note : When using tables with a high number of columns and/or with long field names, be aware of the group_concat_max_len limit, which can cause the data to get truncated.
问题是:
SQL 获取每列的所有信息
SQL 获取所有列名称
The question was :
SQL to get all information for each column
SQL to get all COLUMN NAMES
与 @suganya 发布的答案类似,这并没有直接回答问题,而是单个表的更快替代方案:
Similar to the answer posted by @suganya this doesn't directly answer the question but is a quicker alternative for a single table:
我很久以前写了这个愚蠢的东西,现在仍然时不时地使用它:
https://gist.github .com/kphretiq/e2f924416a326895233d
基本上,它会执行“SHOW TABLES”,然后在每个表上执行“DESCRIBE”,然后将其以降价形式吐出。
只需在“if 名称”下面进行编辑即可。您需要安装 pymysql。
I wrote this silly thing a long time ago and still actually use it now and then:
https://gist.github.com/kphretiq/e2f924416a326895233d
Basically, it does a "SHOW TABLES", then a "DESCRIBE " on each table, then spits it out as markdown.
Just edit below the "if name" and go. You'll need to have pymysql installed.
用一些可读的 php 来支持 Nicola 的答案
Piggybacking on Nicola's answer with some readable php
您可以使用此查询查询特定数据库中每个表的所有列
You can query all columns per table in specific database using this query
您可以使用 information_schema.columns 简单地获取表的所有列,只需添加 group_concat 即可获取逗号分隔的列列表。
You can simply get all columns of a table using information_schema.columns just add group_concat to get a comma separated list of columns.