在mysql获取如何去获取某个表的所有字段名

转载:mym43210

mysql安装成功后可以看到已经存在mysql、information_schema和test这个几个数据库。

information_schema库中有一个名为COLUMNS的表,这个表中记录了数据库中所有表的字段信息。

知道这个表后,获取任意表的字段就只需要一条select语句即可。例如:

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

上述的做法有一点问题,如果多个数据库中存在你想要查询的表名,那么查询的结果会包括全部的字段信息。

通过DESC information_schema.COLUMNS可以看到该表中列名为TABLE_SCHEMA是记录数据库名,因此下面的写法更为严格

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

取字段注释 

Select COLUMN_NAME 列名, DATA_TYPE 字段类型, COLUMN_COMMENT 字段注释  
from INFORMATION_SCHEMA.COLUMNS  
Where table_name = 'companies'  ##表名  
AND table_schema = 'testhuicard'##数据库名  
AND column_name LIKE 'c_name'   ##字段名  
原文地址:https://www.cnblogs.com/isme-zjh/p/13036227.html