从多个表中选择count(*)

如何count(*)从具有结果的两个不同表(分别称为tab1和tab2)中进行选择:


Count_1   Count_2

123       456

我已经试过了:


select count(*) Count_1 from schema.tab1 union all select count(*) Count_2 from schema.tab2

但是我所拥有的是:


Count_1

123

456


慕尼黑8549860
浏览 930回答 3
3回答

慕无忌1623718

SELECT  (        SELECT COUNT(*)        FROM   tab1        ) AS count1,        (        SELECT COUNT(*)        FROM   tab2        ) AS count2FROM    dual

富国沪深

只是因为它略有不同:SELECT 'table_1' AS table_name, COUNT(*) FROM table_1UNIONSELECT 'table_2' AS table_name, COUNT(*) FROM table_2UNIONSELECT 'table_3' AS table_name, COUNT(*) FROM table_3它给出了换位的答案(每张表一行而不是一列),否则我认为这没有太大不同。我认为在性能方面,它们应该是等效的。

肥皂起泡泡

其他略有不同的方法:with t1_count as (select count(*) c1 from t1),     t2_count as (select count(*) c2 from t2)select c1,       c2from   t1_count,       t2_count/select c1,       c2from   (select count(*) c1 from t1) t1_count,       (select count(*) c2 from t2) t2_count/
打开App,查看更多内容
随时随地看视频慕课网APP