使用复合主键的 SQL 连接

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27607516/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-01 03:06:15  来源:igfitidea点击:

SQL join with composite primary key

sqljoincompositecomposite-primary-key

提问by Anubh

I have to join two tables. But in one table primary key is not there,composite primary key is there,means three columns put together uniquely define a row of that table. I have those three columns in the other table too.rest nothing is common. Is there any way to join these two tables.please explain with the help of example

我必须加入两个表。但是在一个表中没有主键,有复合主键,意味着三列放在一起唯一地定义了该表的一行。我在另一个表中也有这三列。其余的没有什么共同之处。有什么办法可以连接这两个表,请举例说明

回答by Eterm

You can use AND in the expression for the ON criteria and demand the fields are all equal there.

您可以在 ON 条件的表达式中使用 AND 并要求所有字段在那里都相等。

SELECT * 
FROM Table1
INNER JOIN Table2
ON Table1.Key1 = Table2.Key1 AND Table1.Key2 = Table2.Key2 AND Table1.Key3 = Table2.Key3