加入多列 [英] join on multiple columns

查看:24
本文介绍了加入多列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个表(表 A 和表 B),我想将它们连接到两个表中的多个列上.

I have two tables (Table A and Table B) which I want to join on multiple columns in both tables.

Table A                         
Col1     Col2                
================            
A11      A21                 
A22      A22              
A33      A23                 

Table B 
Col1     Col2   Val 
=================  
B11     B21     1  
B12     B22     2  
B13     B23     3  

我希望表 A 中的两列都加入表 B 中的 Col1 和 Col2 中的任何一个以获得 Val.

I want both Columns in Table A to join on either of Col1 and Col2 in Table B to get Val.

推荐答案

同意您的示例中没有匹配项.
如果您的意思是两列都在其中,则需要这样的查询或需要重新检查数据设计.

Agree no matches in your example.
If you mean both columns on either then need a query like this or need to re-examine the data design.

    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

这篇关于加入多列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆