当我手动将每个select命令插入DB2时,它会打印所有正确的内容:
但是当我使用下面的代码使得所有5个表都成为一个巨大的表时,它会打印出太多记录:
代码:
SELECT A.PERF_NO, B.A_NAME, C.TCOMP_NAME, D.P_TITLE, E.PDATE from (select PERF_NO from PERFORMS) A \
CROSS JOIN (select A_NAME from ACTOR_BORN_LIVES s, PERFORMS z where s.A_NO=z.PERF_NO) B \
CROSS JOIN (select TCOMP_NAME from THEATER_COMP_PLACE s, PERFORMS z where s.TCOMP_NO=z.TCOMP_NO) C \
CROSS JOIN (select P_TITLE from PLAY s, PERFORMS z where s.P_NO=z.PLAY_NO) D \
CROSS JOIN (select PDATE from PERFORMS s, PLAY z where z.P_NO=s.PLAY_NO) E
对此有何看法?
答案 0 :(得分:1)
CROSS JOIN
将第一个结果集中的每一行连接到第二个结果集中的每一行。这意味着如果您从一个结果中加入5个记录,并从第二个结果集中获得6个结果,则得到5x6 = 30个结果。你真正需要的是INNER JOIN
:
SELECT perf.PERF_NO, born.A_NAME, theat.TCOMP_NAME, pl.P_TITLE, pl.PDATE
FROM PERFORMS perf INNER JOIN ACTOR_BORN_LIVES born ON born.A_NO=perf.PERF_NO
INNER JOIN THEATER_COMP_PLACE theat ON theat.TCOMP_NO=perf.TCOMP_NO
INNER JOIN PLAY pl ON pl.P_NO=perf.PLAY_NO