我有以下問題:
我有一張表,其中包含不同的 ID 和指標對。
Table A:
ID Indicator
----------------------
1 1
1 2
1 3
2 1
2 2
2 3
----------------------
還有另一個名稱不同的表。
Table B:
Names
-------
John
Lea
Peter
--------
我想要的是表 A 中的一列,其中包含每個 ID-Indicator 對的表 B 的名稱:
Outcome:
ID Indicator Names
----------------------------------
1 1 John
1 2 John
1 3 John
2 1 John
2 2 John
2 3 John
1 1 Lea
1 2 Lea
1 3 Lea
2 1 Lea
2 2 Lea
2 3 Lea
1 1 Peter
1 2 Peter
1 3 Peter
2 1 Peter
2 2 Peter
2 3 Peter
----------------------------------
實際上,帶有名稱的表 B 真的很長,因此手動解決方案如下:
SELECT ID, Indicator, Names = 'John'
FROM TableA
UNION
SELECT ID, Indicator, Names = 'Lea'
FROM TableA
是不可行的。這個問題有非手動的解決方案嗎?非常感謝任何幫助!
uj5u.com熱心網友回復:
您希望將 TableA 中的每一行與 TableB 中的每一行連接起來,這被稱為交叉連接,如評論所述。輸出的行數將為 A * B。
如果您想要兩個表中的所有列,則語法很簡單
select *
from TableA cross join TableB
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/405451.html
標籤:
上一篇:如何在多個列上搜索串列?
