SQL 如何使用SQL计算表中的列数?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10084400/
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
How to count the number of columns in a table using SQL?
提问by user6778654
How to count the number of columns in a table using SQL?
如何使用SQL计算表中的列数?
I am using Oracle 11g
我正在使用 Oracle 11g
Please help. t.
请帮忙。吨。
回答by Vikram
select count(*)
from user_tab_columns
where table_name='MYTABLE' --use upper case
Instead of uppercase you can use lower function. Ex: select count(*) from user_tab_columns where lower(table_name)='table_name';
您可以使用lower函数代替大写。例如: select count(*) from user_tab_columns where lower(table_name)='table_name';
回答by Arion
Maybe something like this:
也许是这样的:
SELECT count(*) FROM user_tab_columns WHERE table_name = 'FOO'
this will count number of columns in a the table FOO
这将计算表 FOO 中的列数
You can also just
你也可以
select count(*) from all_tab_columns where owner='BAR' and table_name='FOO';
where the owner is schema and note that Table Names are upper case
其中所有者是架构并注意表名是大写的
回答by Ray K
Old question - but I recently needed this along with the row count... here is a query for both - sorted by row count desc:
老问题 - 但我最近需要这个和行数......这里是两者的查询 - 按行数 desc 排序:
SELECT t.owner,
t.table_name,
t.num_rows,
Count(*)
FROM all_tables t
LEFT JOIN all_tab_columns c
ON t.table_name = c.table_name
WHERE num_rows IS NOT NULL
GROUP BY t.owner,
t.table_name,
t.num_rows
ORDER BY t.num_rows DESC;