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


当前回答

在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 * FROM information_schema.columns
WHERE table_schema = DATABASE()
ORDER BY table_name, ordinal_position

因为我没有足够的代表来评论,这里有一个小的改进(在我看来)尼克rulez的优秀答案:替换WHERE table_schema = 'your_db'与WHERE table_schema = DATABASE()。

最好使用以下查询来获取所有列名 很容易

显示tablename中的列

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

在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>";

列出MySQL表中的所有字段:

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