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


当前回答

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

其他回答

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

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限制,这可能导致数据被截断。

列出MySQL表中的所有字段:

select * 
  from information_schema.columns 
 where table_schema = 'your_DB_name' 
   and table_name = 'Your_tablename'
select column_name from information_schema.columns
where table_schema = 'your_db'
order by table_name,ordinal_position
<?php
        $table = 'orders';
        $query = "SHOW COLUMNS FROM $table";
        if($output = mysql_query($query)):
            $columns = array();
            while($result = mysql_fetch_assoc($output)):
                $columns[] = $result['Field'];
            endwhile;
        endif;
        echo '<pre>';
        print_r($columns);
        echo '</pre>';
?>

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