MySql 获取表的字段名

mysql安装成功后可以看到已经存在mysql、information_schema和test这个几个数据库,information_schema库中有一个名为COLUMNS的表,这个表中记录了数据库中所有表的字段信息。知道这个表后,获取任意表的字段就只需要一条select语句即可。例如:

1 select COLUMN_NAME from information_schema.COLUMNS 
2 where table_name = 'your_table_name';

上述的做法有一点问题,如果多个数据库中存在你想要查询的表名,那么查询的结果会包括全部的字段信息。通过DESC information_schema.COLUMNS可以看到该表中列名为TABLE_SCHEMA是记录数据库名,因此下面的写法更为严格

1 select COLUMN_NAME from information_schema.COLUMNS 
2 where table_name = 'your_table_name' 
3 and table_schema = 'your_db_name';

 原文:http://www.netingcn.com/mysql-column-name.html

原文地址:https://www.cnblogs.com/ImaY/p/5718247.html