【问题标题】:ORACLE SQL DEVELOPER-select count(*) from multiple databasesORACLE SQL DEVELOPER-select count(*) from multiple databases
【发布时间】:2021-11-16 19:35:35
【问题描述】:

我如何从两个不同的数据库(称为 ZEOTA 和 SP)中选择 count(*),结果如下:

Zeota SP
  88  3

我试过这个:

SELECT COUNT(CONSTRAINT_TYPE) NumberOfPrimaryKeys_Zeota 
FROM ALL_CONSTRAINTS
WHERE CONSTRAINT_TYPE = 'P' 
AND 
OWNER = 'ZEOTA';
SELECT COUNT(*) AS NumberOfAttributes_SP
FROM ALL_COL_COMMENTS
WHERE OWNER = 'SP';

但输出显示两个单独的查询结果:

Query result 1: 
    Zeota
      88
Query Result 2:
      SP
      3

但是我正在尝试这样做,但是遇到了问题:

SELECT 
COUNT(DISTINCT TABLE_NAME) AS ZNumOfTables,
COUNT(DISTINCT TABLE_NAME) AS SNumOfTables,
COUNT(DISTINCT TABLE_NAME) AS PNumOfTables
FROM ALL_TAB_COLUMNS
WHERE OWNER = 'SP';
WHERE OWNER = 'ZEOTA';

【问题讨论】:

    标签: oracle count oracle-sqldeveloper


    【解决方案1】:

    一种选择是将您当前的查询用作 CTE,然后交叉连接它们:

    with
    t1 as 
      (select count(constraint_type) num_pk_zeota
       from all_constraints
       where owner = 'ZEOTA'
         and constraint_type = 'PK'
      ),
    t2 as
      (select count(*) num_attr_sp
       from all_col_comments
       where owner = 'SP'
      )
    select a.num_pk_zeota,
           b.num_attr_sp
    from t1 a cross join t2 b;
    

    附:您所说的“数据库”是 Oracle 中的用户(模式)。

    【讨论】:

    • 谢谢!现在我正在添加更多“用户”,但最后一个 select 语句遇到了问题。这就是我的交叉连接,“从 t1 a cross join t2 b and t3 c cross join t4 d;”我收到“SQL 命令未正确结束”
    • 只是from t1 a cross join t2 b cross join t3 c
    猜你喜欢
    • 2014-12-12
    • 1970-01-01
    • 1970-01-01
    • 2023-01-30
    • 1970-01-01
    • 1970-01-01
    • 2014-10-26
    • 1970-01-01
    • 2014-06-17
    相关资源
    最近更新 更多