是否有一种快速的方法从MySQL中所有表中获得所有列名,而不必列出所有表?
当前回答
最好使用以下查询来获取所有列名 很容易
显示tablename中的列
其他回答
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'
<?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 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限制,这可能导致数据被截断。
我很久以前写了这个愚蠢的东西,现在仍然时不时地使用它:
https://gist.github.com/kphretiq/e2f924416a326895233d
基本上,它先执行“SHOW TABLES”,然后对每个表执行“DESCRIBE”,然后将其作为markdown输出。
只要在“if name”下面编辑,就可以了。您需要安装pymysql。