簡體   English   中英

你如何在不使用公共列的情況下連接兩個表?

[英]how do you JOIN two tables without using common column?

Table 1                               Table 2

Column1    Column2    Column3         Column4    Column5    Column3
-------    -------    -------         -------    -------    -------
A          1          a               E          10         a
B          2          b               F          11         b
C          3          c               G          12         c
D          4          d               H          13         d


Table 3                               Table 4  

Column6    Column7    Column4         Column8    Column9    Column6
-------    -------    -------         -------    -------    -------
I          20         E               M          30         I
J          21         F               N          31         J
K          22         G               O          32         K
L          23         H               P          33         L

預期產出

Column1    Column9
-------    -------
C          31
C          32
C          33

如何讓輸出查看上面預期的內容?

這是我目前正在學習的 Oracle SQL 課程。 盡管表具有公共列,但問題要求不要使用公共列,而是使用不同的列來獲得預期的輸出,同時消除重復項。

鑒於發布的結構,此聲明...

 select tableA.column1
        , tableD.column9
 from tableA
      cross join tableD
 where tableA.column1 = 'C'
 and tableD.column9 > 30

...將產生預期的輸出。 顯然,它作為查詢沒有意義,因此它是該問題的恰當答案。

注意:如果 OP 使用適當的數據模型和真實數據編輯他們的問題,那么我當然會編輯我的答案以使其更明智。

當我需要創建報告並將兩個不同的表合並為一個時,這對我有用。 最簡單的,不需要 JOIN。

SELECT table1.column1, table4.column9 FROM table1, table4 WHERE column1 = 'c'

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM