MySQL 如何计算表的列数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10492164/
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-08-31 13:17:26  来源:igfitidea点击:

How do I count columns of a table

mysqlsql

提问by rjmcb

For example :

例如 :

tbl_ifo

id | name  | age | gender 
----------------------------
1  | John  |  15 |  Male
2  | Maria |  18 |  Female
3  | Steph |  19 |  Female
4  | Jay   |  21 |  Male

How can I count the columns of this table using mysql?

如何使用mysql计算此表的列?

回答by swapnesh

SELECT count(*)
FROM information_schema.columns
WHERE table_name = 'tbl_ifo'

回答by John Woo

I think you need also to specify the name of the database:

我认为您还需要指定数据库的名称:

SELECT COUNT(*)
FROM INFORMATION_SCHEMA.COLUMNS
WHERE table_schema = 'SchemaNameHere'
  AND table_name = 'TableNameHere'

if you don't specify the name of your database, chances are it will count all columns as long as it matches the name of your table. For example, you have two database: DBaseA and DbaseB, In DBaseA, it has two tables: TabA(3 fields), TabB(4 fields). And in DBaseB, it has again two tables: TabA(4 fields), TabC(4 fields).

如果您没有指定数据库的名称,只要它与您的表的名称匹配,它就有可能计算所有列。例如,你有两个数据库:DBaseA and DbaseB,在DBaseA,它有两个表:塔巴(3场塔布(4场)。而在DBaseB,它再次有两个表:塔巴(4场TABC(4场)

if you run this query:

如果您运行此查询:

SELECT count(*)
FROM information_schema.columns
WHERE table_name = 'TabA'

it will return 7 because there are two tables named TabA. But by adding another condition table_schema = 'SchemaNameHere':

它将返回 7,因为有两个名为TabA. 但是通过添加另一个条件table_schema = 'SchemaNameHere'

SELECT COUNT(*)
FROM INFORMATION_SCHEMA.COLUMNS
WHERE table_schema = 'DBaseA'
  AND table_name = 'TabA'

then it will only return 3.

那么它只会返回3。

回答by Toivo EU

$cs = mysql_query("describe tbl_info");
$column_count = mysql_num_rows($cs);

Or just:

要不就:

$column_count = mysql_num_rows(mysql_query("describe tbl_info"));

回答by AKZap

To count the columns of your table precisely, you can get form information_schema.columnswith passing your desired Database(Schema) Name and Table Name.

要精确计算表格的列数,您可以information_schema.columns通过传递所需的数据库(架构)名称和表格名称来获取表格。


Reference the following Code:


参考以下代码:

SELECT count(*)
FROM information_schema.columns
WHERE table_schema = 'myDB'  
AND table_name = 'table1';

回答by Douglas.Sesar

I have a more general answer; but I believe it is useful for counting the columns for all tables in a DB:

我有一个更一般的答案;但我相信它对于计算数据库中所有表的列很有用:

SELECT table_name, count(*)
FROM information_schema.columns
GROUP BY table_name;

回答by Ramandeep sohi

Simply use mysql_fetch_assoc and count the array using count() function

只需使用 mysql_fetch_assoc 并使用 count() 函数计算数组

回答by varsha

this query may help you

此查询可能对您有所帮助

SELECT COUNT(COLUMN_NAME) FROM INFORMATION_SCHEMA.COLUMNS WHERE 
TABLE_CATALOG = 'database' AND TABLE_SCHEMA = 'dbo'
AND TABLE_NAME = 'tbl_ifo'

回答by Tinu Mathai

I think you want to know the total entries count in a table! For that use this code..

我想您想知道表中的总条目数!为此使用此代码..

SELECT count( * ) as Total_Entries FROM tbl_ifo;

SELECT count( * ) as Total_Entries FROM tbl_ifo;