如何使用SQL将一列上的值匹配到另一列?我正在匹配ID。我需要两个列都使用相同的ID进行联盟

时间:2017-08-07 14:10:02

标签: sql sql-server

输出需要的示例。

GameID  Name       LegacyGUID      AssociatedGUID
 807    Hujim       567890-ABDE    567890-ABDE

2 个答案:

答案 0 :(得分:1)

您可以通过where子句

来完成
SELECT * FROM TABLE_NAME as t
WHERE  t.column_1 = t.column_2

答案 1 :(得分:0)

你可以self join同桌。这样您就可以从每个表中选择列:

select t.GameName,  t.LegacyGUID,   t.CreatedOn,    t.ModifiedOn,t1.AssociatedGUID
from table_name as T
join table_name as T1 on t.AssociatedGUID = t1.LegacyGUID      

根据您在下面评论中的示例,这里是完整的脚本:

create table #temp
(
GameName varchar(255),
LegacyGUID varchar(255) ,
CreatedOn varchar(255),
ModifiedOn varchar(255),
AssociatedGUID varchar(255) 
)

insert into #temp
values('Pacman','8F1EC35F-5105-DF11-AC31','21:31.0','19:45.0', '375BFE1A-84D8-DF11-82A7') 
,('Sonic','375BFE1A-84D8-DF11-82A7','46:15.0','03:48.0','A64DC346-834E-E011-A2E0 ')


select t1.GameName, t1.LegacyGUID,  t1.CreatedOn,   t1.ModifiedOn,t2.AssociatedGUID
from #temp t1
join #temp t2 on t1.LegacyGUID = t2.AssociatedGUID

drop table #temp

结果:

GameName    LegacyGUID  CreatedOn   ModifiedOn  AssociatedGUID
Sonic   375BFE1A-84D8-DF11-82A7 46:15.0 03:48.0 375BFE1A-84D8-DF11-82A7