Answers:
select * 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限制,这可能导致数据被截断。
<?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>';
?>
问题是:
有没有一种快速方法可以从MySQL的所有表中获取所有列名,而不必列出所有表?
SQL获取每一列的所有信息
select * from information_schema.columns
where table_schema = 'your_db'
order by table_name,ordinal_position
SQL获取所有列名
select COLUMN_NAME from information_schema.columns
where table_schema = 'your_db'
order by table_name,ordinal_position
我很久以前就写了这个愚蠢的东西,但现在仍然时不时地使用它:
https://gist.github.com/kphretiq/e2f924416a326895233d
基本上,它在每个表上执行“ SHOW TABLES”,然后执行“ DESCRIBE”,然后将其作为markdown吐出。
只需在“ if name ” 下方进行编辑即可。您需要安装pymysql。
通过一些可读的PHP搭载Nicola的答案
$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>";