使用SQL语句获取数据库中的字段数? [英] Getting number of fields in a database with an SQL Statement?
本文介绍了使用SQL语句获取数据库中的字段数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何使用SQL语句获取数据库中的字段/条目数量?
How would I get the number of fields/entries in a database using an SQL Statement?
推荐答案
在所有表?假设标准(mssql,mysql,postgres)可以通过information_schema.columns发出查询
mmm all the fields in all the tables? assuming standards (mssql, mysql, postgres) you can issue a query over information_schema.columns
SELECT COUNT(*)
FROM INFORMATION_SCHEMA.COLUMNS
或按表分组:
SELECT TABLE_NAME, COUNT(*)
FROM INFORMATION_SCHEMA.COLUMNS
GROUP BY TABLE_NAME
如果多个模式在同一个数据库中具有相同的表名,则必须包含模式名称(即:dbo.Books,user.Books,company.Books等)。 )否则你会得到错误的结果。所以最佳做法是:
If multiple schemas has the same table name in the same DB, you MUST include schema name as well (i.e: dbo.Books, user.Books, company.Books etc.) Otherwise you'll get the wrong results. So the best practice is:
SELECT TABLE_SCHEMA, TABLE_NAME, COUNT(*)
FROM INFORMATION_SCHEMA.COLUMNS
GROUP BY TABLE_SCHEMA, TABLE_NAME
这篇关于使用SQL语句获取数据库中的字段数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文