Oracle SQL 使用连接创建重复行

发布于 2024-12-07 09:03:57 字数 544 浏览 0 评论 0原文

我想创建重复的行作为输出:首先我想要该行,然后再次将同一行与另一个表连接,如下所示:

table A with fieldA (and lots of others)

table B with fieldB (and lots of others)

输出:

fieldA1          (and all the fileds from tableA)      
fieldA1 fieldB1  (and all the fields from tableA and tableB joined) 
filedA1 fieldB2  
fieldA2 
fieldA2 fieldB8   
filedA2 fieldB9   
. . .

我正在考虑使用联合,但随后我会复制 tableA 的非常复杂的选择以获取 tableA 和 tableB 的行(tableA 是其他表的并集,我只是针对问题简化了它)。有没有“更干净”的解决方案?我知道这是一个不寻常的问题,所以我会感谢任何想法或想法。

预先非常感谢您!

I would like to create duplicated rows as an output: first I would like to have the row, and then the same row again joined with another table, like this:

table A with fieldA (and lots of others)

table B with fieldB (and lots of others)

and the output:

fieldA1          (and all the fileds from tableA)      
fieldA1 fieldB1  (and all the fields from tableA and tableB joined) 
filedA1 fieldB2  
fieldA2 
fieldA2 fieldB8   
filedA2 fieldB9   
. . .

I was thinking about using union, but then I would have to duplicate the very complicated select of tableA to get the rows of tableA and tableB (tableA is union of other tables, I just simplified it for the question). Is there any 'cleaner' solution to this? I know it is an unusual question, so I would appriciate any thougts or ideas.

Thank you very much in advance!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

忆伤 2024-12-14 09:03:57

修改 Benoit 的答案以使用公用表表达式:

WITH A as (
 your select for "A"
)
SELECT A.fieldA, B.fieldB, A.*, B.*
  FROM A LEFT JOIN B ON 1 = 0
UNION ALL
SELECT A.fieldA, B.fieldB, A.*, B.*
  FROM A JOIN B ON (join condition)

Modify Benoit's answer to use a common table expression:

WITH A as (
 your select for "A"
)
SELECT A.fieldA, B.fieldB, A.*, B.*
  FROM A LEFT JOIN B ON 1 = 0
UNION ALL
SELECT A.fieldA, B.fieldB, A.*, B.*
  FROM A JOIN B ON (join condition)
断桥再见 2024-12-14 09:03:57

使用:

SELECT A.*, B.*
  FROM A LEFT JOIN B ON 1 = 0
 UNION ALL
SELECT A.*, B.*
  FROM A INNER JOIN B ON (join condition)

Use:

SELECT A.*, B.*
  FROM A LEFT JOIN B ON 1 = 0
 UNION ALL
SELECT A.*, B.*
  FROM A INNER JOIN B ON (join condition)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文