是否有一种快速的方法从MySQL中所有表中获得所有列名,而不必列出所有表?


当前回答

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

https://gist.github.com/kphretiq/e2f924416a326895233d

基本上,它先执行“SHOW TABLES”,然后对每个表执行“DESCRIBE”,然后将其作为markdown输出。

只要在“if name”下面编辑,就可以了。您需要安装pymysql。

其他回答

在Nicola的回答上加上一些可读的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>";

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

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

列出MySQL表中的所有字段:

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

类似于@suganya发布的答案,这并没有直接回答问题,但对于单个表来说是一个更快的选择:

DESCRIBE column_name;