mysql中怎么查询表中的字段个数?

JAVA_STU 2009-12-09 04:09:53
mysql中怎么查询表中的字段个数?
...全文
1736 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
JAVA_STU 2009-12-09
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 wwwwb 的回复:]
select count(*) from information_schema.columns where table_schema='databasename' and table_
name='tablename' ;
[/Quote]
查出来了,3q。
wwwwb 2009-12-09
  • 打赏
  • 举报
回复
select count(*) from information_schema.columns where table_schema='databasename' and table_
name='tablename' ;
ACMAIN_CHM 2009-12-09
  • 打赏
  • 举报
回复
方法二,通过系统表information_schema.`COLUMNS` ( mysql5以上版本支持)



mysql> select count(*) from information_schema.`COLUMNS`
-> where TABLE_SCHEMA='csdn'
-> and TABLE_NAME='ysks';
+----------+
| count(*) |
+----------+
| 8 |
+----------+
1 row in set (0.06 sec)

mysql>
ACMAIN_CHM 2009-12-09
  • 打赏
  • 举报
回复
方法一,在你的程序中直接
desc tablename

然后总行数就是你的字段数。

mysql> desc ysks;
+-------+---------------+-----
| Field | Type | Null
+-------+---------------+-----
| 单号 | int(11) | YES
| 金额 | decimal(10,2) | YES
| 已收 | decimal(10,2) | YES
| 日期 | bigint(20) | YES
| 名称 | varchar(10) | YES
| 余额 | decimal(10,2) | YES
| 备注 | varchar(10) | YES
| 品名 | varchar(10) | YES
+-------+---------------+-----
8 rows in set (0.06 sec)

mysql> select FOUND_ROWS();
+--------------+
| FOUND_ROWS() |
+--------------+
| 8 |
+--------------+
1 row in set (0.06 sec)

mysql>
vipper23 2009-12-09
  • 打赏
  • 举报
回复
select count(1) from columns where table_name =要查的表的名字;
wwwwb 2009-12-09
  • 打赏
  • 举报
回复
如果是5以上,可以直接查询系统表
sELECT table_name, table_type, engine
FROM information_schema.tables
WHERE table_schema = 'databasename' and table_name='tablename'
ORDER BY table_name DESC;

56,681

社区成员

发帖
与我相关
我的任务
社区描述
MySQL相关内容讨论专区
社区管理员
  • MySQL
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧