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


当前回答

您可以使用information_schema简单地获取一个表的所有列。列只需添加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'

其他回答

<?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>';
?>
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()。

列出MySQL表中的所有字段:

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

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

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

您可以使用information_schema简单地获取一个表的所有列。列只需添加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'