如何在SQL中按多列联接两个表? [英] How to Join two tables by multiple columns in SQL?

查看:263
本文介绍了如何在SQL中按多列联接两个表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个表EvalulationValue

在两个表中都有四列.但是四个中的三个是相同的.换句话说,它们都具有CaseNumFileNumActivityNum.除了这些列之外,Evaluation还具有列Grade,而Value还具有列Score.

In both tables, there are four columns. But three of the four are the same. In other words, they both have CaseNum, FileNum, ActivityNum. In addition to those columns, Evaluation has column Grade and Value has column Score.

我想通过CaseNumFileNumActivityNum将两者合并到一个表中,所以我有一个新的5列表,其中同时包含ValueScore.

I want to merge the two into one table by CaseNum, FileNum, ActivityNum so I have a new table of 5 columns with both Value and Scorein it.

我可以多次使用Inner Join来执行此操作吗?

Can I use Inner Join multiple times to do this?

推荐答案

答案是肯定的:您可以使用内部联接 您可以在通用列"上创建联接

Answer is Yes: You can Use Inner join you can create join on Common Columns

select E.CaseNum, E.FileNum, E.ActivityNum,E.Grade,V.score from Evalulation E
inner join Value V
ON E.CaseNum=V.CaseNum and
E.FileNum=V.FileNum and 
E.ActivityNum=V.ActivityNum

创建表格

Create table MyNewTab(CaseNum int, FileNum int, 
ActivityNum int,Grade int,score varchar(100))

插入值

Insert into MyNewTab Values(CaseNum, FileNum, ActivityNum,Grade,score)
select E.CaseNum, E.FileNum, E.ActivityNum,E.Grade,V.score from Evalulation E
inner join Value V
ON E.CaseNum=V.CaseNum and
E.FileNum=V.FileNum and 
E.ActivityNum=V.ActivityNum

这篇关于如何在SQL中按多列联接两个表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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