简体   繁体   English

如何使用相同的主键联接两个表?

[英]How to join the two tables with the same primary key?

How to join the two tables with the same primary key? 如何使用相同的主键联接两个表?

Table1: 表格1:

Item_ID   Name   Date
I1        ABC    21-03-2018
I2        EFG    21-04-2018
I3        XYZ    21-05-2018
I4        LKJ    21-06-2018

Table2: 表2:

Item_ID   Cost   
I1        21    
I1        54
I1        27
I1        32
I2        65 
I2        75 
I3        45  
I3        46
I4        34
I4        74

Please try this. 请尝试这个。

SELECT * FROM table1 A
INNER JOIN table2 B
ON A.Item_Id = B.Item_Id

Use joins : 使用联接

SELECT name, date, cost from Table1, Table2 INNER JOIN table2 AS two ON two.item_id=item_id

Next time, please make a search on google. 下次,请在Google上进行搜索。 There are many easy examples there. 那里有很多简单的例子。

Please try following query. 请尝试以下查询。

You can Join the tables through "INNER JOIN" . 您可以通过“ INNER JOIN”联接表。 t1 & t2 is alias for Tables. t1和t2是表的别名。 So you can read the same column name. 因此,您可以读取相同的列名。

SELECT * 
FROM Table1 as t1
INNER JOIN Table2 as t2 
 ON t1.Item_ID=t2.Item_ID

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在同一主键上联接3个表? - How to join 3 tables on same primary key? 如何在没有主键或唯一键的情况下联接两个表? - How to join two tables without primary key or unique key? 使用同样也是自动递增的相同PRIMARY键联接2个表 - Join 2 tables with same PRIMARY key that is autoincrementing too 当有两个外键引用同一个主键时,如何在 SQL 中的两个表上进行连接? 这让我发疯 - How do I make a join on two tables in SQL when there are two foreign keys referencing the same primary key? It's driving me insane 当一个表的主键与另一个表的主键包含但不包含在一起时,如何连接表? - How to join tables when primary key of one table is not the same as but contained within the primary key of the other table? 如何将数据发送到具有相同用户ID作为主键的两个表? - How to send data to two tables with same user ID as primary key? 如何插入两个具有相同主键的表? - How to insert two tables having same primary key? 如何一次向共享相同主键的两个表中插入一行? - How to insert a row to two tables sharing the same primary key at once? 如何在使用内部联接合并两个表时将列设置为PRIMARY KEY(两个联接表中没有主键) - How to set a column to PRIMARY KEY while merging two tables using inner join?(there is no primary key in the two joining tables) 数据库规范化-具有相同主键的两个表? - Database Normalisation - Two tables with the same primary key?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM