我有两个表(表A和表B),我想在两个表中的多个列上连接.
Table A
Col1 Col2
================
A11 A21
A22 A22
A33 A23
Table B
Col1 Col2 Val
=================
B11 B21 1
B12 B22 2
B13 B23 3
Run Code Online (Sandbox Code Playgroud)
我希望表A中的两列都连接到表B中的Col1和Col2中,以获得Val.
pap*_*zzo 53
在您的示例中同意不匹配.
如果你的意思是两个列都需要这样的查询或者需要重新检查数据设计.
Select TableA.Col1, TableA.Col2, TableB.Val
FROM TableA
INNER JOIN TableB
ON TableA.Col1 = TableB.Col1 OR TableA.Col2 = TableB.Col2
OR TableA.Col2 = TableB.Col1 OR TableA.Col1 = TableB.Col2
Run Code Online (Sandbox Code Playgroud)
DRa*_*app 22
其他查询都基于符合条件的任何一个条件,它将返回一条记录...如果你想确保表A的BOTH列匹配,你将不得不做类似......
select
tA.Col1,
tA.Col2,
tB.Val
from
TableA tA
join TableB tB
on ( tA.Col1 = tB.Col1 OR tA.Col1 = tB.Col2 )
AND ( tA.Col2 = tB.Col1 OR tA.Col2 = tB.Col2 )
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
151117 次 |
| 最近记录: |