【问题标题】:How to count the number of columns in a table using SQL? [duplicate]如何使用 SQL 计算表中的列数? [复制]
【发布时间】:2012-04-22 11:40:48
【问题描述】:

如何使用 SQL 统计表中的列数?

我正在使用 Oracle 11g

请帮忙。 t.

【问题讨论】:

标签: sql oracle


【解决方案1】:
select count(*) 
from user_tab_columns
where table_name='MYTABLE' --use upper case

您可以使用 lower 函数代替大写。 前任: select count(*) from user_tab_columns where lower(table_name)='table_name';

【讨论】:

  • user_tab_columns 表中没有任何内容
  • 对我不起作用
  • 它工作,但表名区分大小写。
  • 是否有标识架构的列?如果在不同的架构中有两个同名的表怎么办——“user_table_columns”会显示哪一个?
  • Nvm - 只是意识到在这种情况下应该使用'all_tab_columns'
【解决方案2】:

可能是这样的:

SELECT count(*) FROM user_tab_columns WHERE table_name = 'FOO'

这将计算表 FOO 中的列数

你也可以

select count(*) from all_tab_columns where owner='BAR' and table_name='FOO';

所有者是架构,注意表名是大写的

【讨论】:

  • 如果我不仅需要计数值,还需要特定的列值,是否可以轻松附加它?
【解决方案3】:

老问题 - 但我最近需要这个以及行数......这是两者的查询 - 按行数 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; 

【讨论】:

    猜你喜欢
    • 2013-10-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-07-02
    相关资源
    最近更新 更多