MySQL 查找表中的列数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/658395/
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
Find the number of columns in a table
提问by praveenjayapal
It is possible to find the number of rows in a table:
可以找到表中的行数:
select count(*) from tablename
Is it possible to find the number of columns in a table?
是否可以找到表中的列数?
回答by Nathan Koop
SELECT COUNT(*)
FROM INFORMATION_SCHEMA.COLUMNS
WHERE table_catalog = 'database_name' -- the database
AND table_name = 'table_name'
回答by kavitha
SELECT COUNT(COLUMN_NAME)
FROM INFORMATION_SCHEMA.COLUMNS
WHERE
TABLE_CATALOG = 'Database name'
AND TABLE_SCHEMA = 'dbo'
AND TABLE_NAME = 'table name'
回答by Rahul Yadav
SELECT count(*)
FROM information_schema.columns
WHERE table_name = 'Your_table_name';
Note: Your_table_name should be replaced by your actual table name
注意:Your_table_name 应替换为您的实际表名
回答by Himanshu
Using JDBC in Java:
在 Java 中使用 JDBC:
String quer="SELECT * FROM sample2";
Statement st=con.createStatement();
ResultSet rs=st.executeQuery(quer);
ResultSetMetaData rsmd = rs.getMetaData();
int NumOfCol=0;
NumOfCol=rsmd.getColumnCount();
System.out.println("Query Executed!! No of Colm="+NumOfCol);
回答by itb564
Its been little late but please take it from me...
有点晚了,但请把它从我这里拿走...
In the editor(New Query) by select the database objectit can be a table too, if we use the Shortcut Key Alt+F1we will get all the information of the object and I think will solve your problem as well.
在编辑器(新查询)中通过选择数据库对象它也可以是一个表,如果我们使用快捷键Alt+F1我们将获得该对象的所有信息,我认为也可以解决您的问题。
回答by Ketul Rathod
SELECT count(*) FROM information_schema.`COLUMNS` C
WHERE table_name = 'your_table_name'
AND TABLE_SCHEMA = "your_db_name"
回答by ambodi
Well I tried Nathan Koop's answer and it didn't work for me. I changed it to the following and it did work:
好吧,我尝试了 Nathan Koop 的回答,但对我不起作用。我将其更改为以下内容并且确实有效:
SELECT COUNT(*)
FROM INFORMATION_SCHEMA.COLUMNS
WHERE table_name = 'table_name'
It also didn't work if I put USE 'database_name'
nor WHERE table_catalog = 'database_name' AND table_name' = 'table_name'
. I actually will be happy to know why.
如果我把USE 'database_name'
nor 也行不通WHERE table_catalog = 'database_name' AND table_name' = 'table_name'
。我实际上会很高兴知道为什么。
回答by jerryhung
Or use the sys.columns
或者使用 sys.columns
--SQL 2005
SELECT *
FROM sys.columns
WHERE OBJECT_NAME(object_id) = 'spt_values'
-- returns 6 rows = 6 columns
--SQL 2000
SELECT *
FROM syscolumns
WHERE OBJECT_NAME(id) = 'spt_values'
-- returns 6 rows = 6 columns
SELECT *
FROM dbo.spt_values
-- 6 columns indeed
回答by Salim Khan
It is possible to find the number of columns in a table just by using 3 simple lines of PHP code.
只需使用 3 行简单的 PHP 代码,就可以找到表中的列数。
$sql="SELECT * FROM table";
$query=mysqli_query($connect_dude,$sql);
$num=mysqli_num_fields($query);
$num
would return the number of columns
on a given table in this case.
$num
columns
在这种情况下,将返回给定表上的数量。
Hopefully,it would help others.
希望它会帮助其他人。
回答by abhid89
SELECT COUNT(COLUMN_NAME) FROM INFORMATION_SCHEMA.COLUMNS WHERE
TABLE_CATALOG = 'database_name' AND TABLE_SCHEMA = 'dbo'
AND TABLE_NAME = 'table_name'