【问题标题】:Return an entire recordset x times (where x is the number of rows in another table)返回整个记录集 x 次(其中 x 是另一个表中的行数)
【发布时间】:2015-12-28 05:19:46
【问题描述】:

假设我有 2 张桌子。

TableA 只有一列,看起来像这样(它总是只有一列,但该列中的值会随着时间而变化):

TableA
------
Column1:
   A
   G
   T
   R

表 B 有很多列(但这些列都不会匹配表 A 的 column1 中的值)。即

TableB
------
Column1:     Column2:      Column3:    
   65           xyz          fgdsg
   97           tmkl          sjg
   184          soi           trhd

我希望能够在查询中返回以下结果集:

 TabA.Column1:   TabB.Column1:   TabB.Column2:      TabB.Column3:
 -------------   ------------     ------------        ----------    
       A              65           xyz                  fgdsg
       A              97           tmkl                  sjg
       A              184          soi                   trhd
       G              65           xyz                  fgdsg
       G              97           tmkl                  sjg
       G              184          soi                   trhd
       T              65           xyz                  fgdsg
       T              97           tmkl                  sjg
       T              184          soi                   trhd
       R              65           xyz                  fgdsg
       R              97           tmkl                  sjg
       R              184          soi                   trhd

为了做到这一点,我想联合 TableB x 次,其中 x 是 TableA 的计数,但我认为您不能仅在 SQL 中动态创建 x 个联合。我只是在寻找 SQL 代码中的解决方案(它可能包含使用预定义的 PL/SQL 函数)。

【问题讨论】:

  • 只是“交叉连接”不起作用? (不需要 where 子句)。

标签: sql database oracle


【解决方案1】:

只是一个CROSS JOIN 不起作用?交叉连接会产生输入集的Cartesian product

SELECT * FROM TableA CROSS JOIN TableB

愉快的编码

【讨论】:

    【解决方案2】:

    您想要的是交叉连接。连接从表 A 到表 B 的所有内容。

    SELECT * FROM TableA  
    CROSS JOIN TableB
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-11-03
      • 2012-12-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-04-30
      • 1970-01-01
      相关资源
      最近更新 更多