mysql获取某个表的所有字段名

原文链接
http://blog.sina.com.cn/s/blog_61e65d1701017m6q.html

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';    (自己没测)

 

posted on 2017-04-19 19:34  _jackie  阅读(33115)  评论(1编辑  收藏  举报

导航