是否有一种快速的方法从MySQL中所有表中获得所有列名,而不必列出所有表?
select column_name from information_schema.columns
where table_schema = 'your_db'
order by table_name,ordinal_position
列出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 * FROM information_schema.columns
WHERE table_schema = DATABASE()
ORDER BY table_name, ordinal_position
因为我没有足够的代表来评论,这里有一个小的改进(在我看来)尼克rulez的优秀答案:替换WHERE table_schema = 'your_db'与WHERE table_schema = DATABASE()。
我很久以前写了这个愚蠢的东西,现在仍然时不时地使用它:
https://gist.github.com/kphretiq/e2f924416a326895233d
基本上,它先执行“SHOW TABLES”,然后对每个表执行“DESCRIBE”,然后将其作为markdown输出。
只要在“if name”下面编辑,就可以了。您需要安装pymysql。
如果它对其他人有用,这将为您提供每个表中以逗号分隔的列列表:
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限制,这可能导致数据被截断。
在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所有表中获得所有的列名,而不必列出所有的表?
SQL来获取每列的所有信息
select * from information_schema.columns
where table_schema = 'your_db'
order by table_name,ordinal_position
获取所有的列名
select COLUMN_NAME from information_schema.columns
where table_schema = 'your_db'
order by table_name,ordinal_position
您可以使用此查询查询特定数据库中每个表的所有列
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'
推荐文章
- 如何通过查询在MySQL中获得数据库结构?
- MySQL删除表中的所有行,并将ID重置为零
- 在准备语句中使用“like”通配符
- MySQL中的表名是否区分大小写?
- 库未加载:libmysqlclient.16。在OS X 10.6上使用mysql2 gem运行'rails server'时出现dylib错误
- 如何知道MySQL表最近一次更新?
- 在MySQL中的一个查询中更新多个具有不同值的行
- 如果表存在则删除表并创建它,如果不存在则创建它
- MySQL OR与IN性能
- 将值从同一表中的一列复制到另一列
- 删除id与其他表不匹配的sql行
- MySQL CPU使用率高
- INT和VARCHAR主键之间有真正的性能差异吗?
- 拒绝访问;您需要(至少一个)SUPER特权来执行此操作
- 从存储引擎得到错误28