简体   繁体   English

在SQL Server中将两个表联接成1个表

[英]Joining two table into 1 table in SQL Server

Here I have two tables. 我这里有两个桌子。 One is called table1 and has a column word , which is of datatype varchar(500) and looks like 一个叫做table1 ,有一个列word ,它的数据类型为varchar(500) ,看起来像

a
pen
book
like
nice
.....

Another table is table2 and has a column meaning of datatype ntext and looks like 另一个表是table2 ,其列meaning为数据类型ntext ,看起来像

 one
 a long thin object used for writing  
 reading material 
 to enjoy  
 pleasant
 .......   

Now I want to join table1 and table2 and with them make a another table table3 现在我想加入table1和table2并与它们一起创建另一个表table3

table3 will have two columns that are word and meaning " column and will look like table3将有两列,分别是wordmeaning “”列,看起来像

a      one
pen    a long thin object used for writing 
book   reading material 
like   to enjoy 
nice   pleasant
.....  .......

How can I do that in SQL Server? 如何在SQL Server中做到这一点?

You better have a PRIMARY KEY column in table1 (lets call it terms) and a FOREIGN KEY column in table2 (lets call it definitions). 最好在表1中有一个PRIMARY KEY列(可以称为术语),在表2中有一个FOREIGN KEY列(可以称为定义)。 Then you can join both tables using these columns to get your result 然后,您可以使用这些列将两个表连接起来以获得结果

SELECT term, definition
  FROM terms t LEFT JOIN 
       definitions d ON t.termid = d.termid

Here is sqlfiddle 这是sqlfiddle

IMHO there is no need for table3. 恕我直言,不需要table3。 You just use a query like this to get desired result. 您只需使用这样的查询即可获得所需的结果。

This way if need be you can store more than one definition per term. 这样,如果需要,您可以在每个术语中存储多个定义。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM