【问题标题】:Joining two table into 1 table in SQL Server在 SQL Server 中将两个表加入到 1 个表中
【发布时间】:2013-02-17 06:19:56
【问题描述】:

这里有两张桌子。一个叫做table1,有一列word,它的数据类型是varchar(500),看起来像

a
pen
book
like
nice
.....

另一个表是table2,有一个列meaning,数据类型为ntext,看起来像

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

现在我想加入 table1 和 table2 并与他们创建另一个表 table3

table3 将有两列 wordmeaning" 列,看起来像

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

如何在 SQL Server 中做到这一点?

【问题讨论】:

标签: c# sql-server visual-studio-2010 join jointable


【解决方案1】:

您最好在 table1 中有一个 PRIMARY KEY 列(我们称之为术语)和一个 FOREIGN KEY 在 table2 中的列(我们称之为定义)。然后你可以使用这些列连接两个表来得到你的结果

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

这里是sqlfiddle

恕我直言,不需要 table3。您只需使用这样的查询即可获得所需的结果。

这样,如果需要,您可以为每个术语存储多个定义。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多