如何获取所有列' MySQL 中所有表的名称?

发布于 2024-10-31 20:28:52 字数 48 浏览 7 评论 0原文

有没有一种快速方法可以从 MySQL 中的所有表中获取所有列名,而不必列出所有表?

Is there a fast way of getting all column names from all tables in MySQL, without having to list all the tables?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(11

呢古 2024-11-07 20:28:52
select column_name from information_schema.columns
where table_schema = 'your_db'
order by table_name,ordinal_position
select column_name from information_schema.columns
where table_schema = 'your_db'
order by table_name,ordinal_position
冧九 2024-11-07 20:28:52

要列出 MySQL 中表的所有字段:

select * 
  from information_schema.columns 
 where table_schema = 'your_DB_name' 
   and table_name = 'Your_tablename'

To list all the fields from a table in MySQL:

select * 
  from information_schema.columns 
 where table_schema = 'your_DB_name' 
   and table_name = 'Your_tablename'
够运 2024-11-07 20:28:52

最好使用以下查询来获取所有列名称
轻松

显示表名中的列

it is better that you use the following query to get all column names
easily

Show columns from tablename

め七分饶幸 2024-11-07 20:28:52
SELECT * FROM information_schema.columns
WHERE table_schema = DATABASE()
ORDER BY table_name, ordinal_position

由于我没有足够的代表来发表评论,因此(在我看来)对 nick Rulez 的出色答案做了一个小小的改进:将 WHERE table_schema = 'your_db' 替换为 WHERE table_schema = DATABASE()

SELECT * FROM information_schema.columns
WHERE table_schema = DATABASE()
ORDER BY table_name, ordinal_position

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' with WHERE table_schema = DATABASE().

偏闹i 2024-11-07 20:28:52

如果它对其他人有用,这将为您提供每个表中以逗号分隔的列列表:

SELECT table_name,GROUP_CONCAT(column_name ORDER BY ordinal_position)
FROM information_schema.columns
WHERE table_schema = DATABASE()
GROUP BY table_name
ORDER BY table_name

注意:当使用具有大量列和/或长字段名称的表时,请注意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:

SELECT table_name,GROUP_CONCAT(column_name ORDER BY ordinal_position)
FROM information_schema.columns
WHERE table_schema = DATABASE()
GROUP BY table_name
ORDER BY table_name

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.

转身以后 2024-11-07 20:28:52

问题是:

是否有一种快速方法可以从 MySQL 中的所有表中获取所有列名称,而无需列出所有表?

SQL 获取每列的所有信息

select * from information_schema.columns
where table_schema = 'your_db'
order by table_name,ordinal_position

SQL 获取所有列名称

select COLUMN_NAME from information_schema.columns
where table_schema = 'your_db'
order by table_name,ordinal_position

The question was :

Is there a fast way of getting all COLUMN NAMES from all tables in MySQL, without having to list all the tables?

SQL to get all information for each column

select * from information_schema.columns
where table_schema = 'your_db'
order by table_name,ordinal_position

SQL to get all COLUMN NAMES

select COLUMN_NAME from information_schema.columns
where table_schema = 'your_db'
order by table_name,ordinal_position
紫竹語嫣☆ 2024-11-07 20:28:52

@suganya 发布的答案类似,这并没有直接回答问题,而是单个表的更快替代方案:

DESCRIBE column_name;

Similar to the answer posted by @suganya this doesn't directly answer the question but is a quicker alternative for a single table:

DESCRIBE column_name;
日裸衫吸 2024-11-07 20:28:52

我很久以前写了这个愚蠢的东西,现在仍然时不时地使用它:

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.

雨后彩虹 2024-11-07 20:28:52

用一些可读的 php 来支持 Nicola 的答案

$a = mysqli_query($conn,"select * from information_schema.columns
where table_schema = 'your_db'
order by table_name,ordinal_position");
$b = mysqli_fetch_all($a,MYSQLI_ASSOC);
$d = array();
foreach($b as $c){
    if(!is_array($d[$c['TABLE_NAME']])){
        $d[$c['TABLE_NAME']] = array();
    }
    $d[$c['TABLE_NAME']][] = $c['COLUMN_NAME'];
}
echo "<pre>",print_r($d),"</pre>";

Piggybacking on Nicola's answer with some readable php

$a = mysqli_query($conn,"select * from information_schema.columns
where table_schema = 'your_db'
order by table_name,ordinal_position");
$b = mysqli_fetch_all($a,MYSQLI_ASSOC);
$d = array();
foreach($b as $c){
    if(!is_array($d[$c['TABLE_NAME']])){
        $d[$c['TABLE_NAME']] = array();
    }
    $d[$c['TABLE_NAME']][] = $c['COLUMN_NAME'];
}
echo "<pre>",print_r($d),"</pre>";
彼岸花ソ最美的依靠 2024-11-07 20:28:52

您可以使用此查询查询特定数据库中每个表的所有列

select
    TABLE_SCHEMA,
    TABLE_NAME,
    COLUMN_NAME
from
    information_schema.columns
where
    table_schema = 'database_name'
order by 
    table_name,
    ordinal_position;

You can query all columns per table in specific database using this query

select
    TABLE_SCHEMA,
    TABLE_NAME,
    COLUMN_NAME
from
    information_schema.columns
where
    table_schema = 'database_name'
order by 
    table_name,
    ordinal_position;
情绪 2024-11-07 20:28:52

您可以使用 information_schema.columns 简单地获取表的所有列,只需添加 group_concat 即可获取逗号分隔的列列表。

select group_concat( column_name ) as ColumnNames from information_schema.columns where table_schema = 'ur_db_name' and table_name = 'ur_tbl_name'

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.

select group_concat( column_name ) as ColumnNames from information_schema.columns where table_schema = 'ur_db_name' and table_name = 'ur_tbl_name'
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文