使用 SQL 语句获取数据库中的字段数?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1033726/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-01 02:31:02  来源:igfitidea点击:

Getting number of fields in a database with an SQL Statement?

sqldatabasegetfield

提问by Ash

How would I get the number of fields/entries in a database using an SQL Statement?

如何使用 SQL 语句获取数据库中的字段/条目数?

回答by Jhonny D. Cano -Leftware-

mmm all the fields in all the tables? assuming standards (mssql, mysql, postgres) you can issue a query over information_schema.columns

mmm 所有表中的所有字段?假设标准(mssql、mysql、postgres),您可以对 information_schema.columns 发出查询

  SELECT COUNT(*) 
  FROM INFORMATION_SCHEMA.COLUMNS 

Or grouped by table:

或按表分组:

  SELECT TABLE_NAME, COUNT(*) 
  FROM INFORMATION_SCHEMA.COLUMNS 
  GROUP BY TABLE_NAME

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:

如果多个模式在同一个数据库中具有相同的表名,则还必须包括模式名(即:dbo.Books、user.Books、company.Books 等),否则会得到错误的结果。所以最好的做法是:

SELECT TABLE_SCHEMA, TABLE_NAME, COUNT(*) 
FROM INFORMATION_SCHEMA.COLUMNS 
GROUP BY TABLE_SCHEMA, TABLE_NAME

回答by SQLMenace

try this, this will exclude views, leave the where clause out if you want views

试试这个,这将排除视图,如果你想要视图,请保留 where 子句

  select count(*) from information_schema.columns c
join information_schema.tables t on c.table_name = t.table_name
and t.table_type = 'BASE TABLE'

回答by Scott Ivey

Sounds like this is what you need.

听起来这就是你所需要的。

select CountOfFieldsInDatabase = count(*)
from   information_schema.columns

回答by hye ji

select count(column_name) from information_schema.columns 
where table_name = **name of your table here **

回答by jcansell

Just for any other readers who are googling...

仅适用于使用谷歌搜索的任何其他读者......

There are several non-SQL solutions, that may be useful to the user.. here's 2 that I use.

有几个非 SQL 解决方案,可能对用户有用..这是我使用的 2 个。

Example 1: Access VBA:

示例 1:访问 VBA:

'Microsoft Access VBA
Function Count_Fields(Table_Name As String) As Integer
    Dim myFieldCount As Integer
    Dim db As DOA.Database
    Dim rs As DAO.Recordset
    Set db = CurrentDb
    Set rs = db.OpenRecordset(Table_Name, dbOpenDynaset)
    myFieldCount = rs.Fields.Count
    'return the count
    Count_Fields = myFieldCount
    'tidy up
    Set rs = Nothing
    Set db = Nothing
End Function

Example 2: PHP 5.1:

示例 2:PHP 5.1:

    <?php
    // PHP5 Implementation - uses MySQLi.
    function countFields ($tableName) { 
    $db = new mysqli('myserver.me.com', 'user' ,'pass', 'databasename');
    if(!$db) {
        echo 'ERROR: Could not connect to the database.';
        } 
    else {
        $rs->$db->query("SELECT?*?FROM?".$tableName.");
        $fieldCount = $rs->field_count;
        }
    return $fieldCount;
?>

please excuse any typo's in the above - hope someone finds this useful

请原谅上面的任何错字 - 希望有人觉得这有用