关于一次打印3个表的SQL查询

时间:2015-03-13 20:29:50

标签: sql db2

代码:

 select count(*) AS "NR_COMEDIES" from COMEDY

 select count(*) AS "NR_DRAMAS" from DRAMA 

 select count(*) AS "NR_TRAGEDIES" from TRAGEDY

我试图将所有3个表的计数功能打印为一个大表。

每个表都有自己的以上命名的列。如何在一张巨大的桌子上制作所有3张桌子?

我尝试使用union但这不起作用。

2 个答案:

答案 0 :(得分:2)

CROSS JOIN可能适合您

SELECT A.NR_COMEDIES,
       B.NR_DRAMAS,
       C.NR_TRAGEDIES
FROM   (SELECT Count(*) AS "NR_COMEDIES"
        FROM   COMEDY) A
       CROSS JOIN (SELECT Count(*) AS "NR_DRAMAS"
                   FROM   DRAMA)B
       CROSS JOIN (SELECT Count(*) AS "NR_TRAGEDIES"
                   FROM   TRAGEDY) C 

或在所有查询之间执行Cartesian product

SELECT A.NR_COMEDIES,
       B.NR_DRAMAS,
       C.NR_TRAGEDIES
FROM   (SELECT Count(*) AS "NR_COMEDIES"
        FROM   COMEDY) A,
       (SELECT Count(*) AS "NR_DRAMAS"
        FROM   DRAMA)B,
       (SELECT Count(*) AS "NR_TRAGEDIES"
        FROM   TRAGEDY) C 

答案 1 :(得分:2)

3个子选择...

select
    (select count(*) from COMEDY) as "NR_COMEDIES",

    (select count(*) from DRAMA) AS "NR_DRAMAS",

    (select count(*) from TRAGEDY) AS "NR_TRAGEDIES"
from
    SYSIBM.SYSDUMMY1