产品关系需要Sql Query帮助

时间:2012-09-11 18:57:47

标签: sql sql-server sql-server-2008

我有一个产品表,其中包含以下信息。

ID ProductId Name OtherData
1 0 A data1 2 0 B data2 3 1 A1 NULL 4 1 A2 NULL

我需要详细信息所有数据ProductId与ID列的关系。

我需要结果如下

ID ProductId Name OtherData
1 0 A data1 2 0 B data2 3 1 A1 data1 4 1 A2 data1

我应该使用什么样的联接或查询?

1 个答案:

答案 0 :(得分:2)

SELECT s.ID, s.ProductId, s.Name, 
  OtherData = COALESCE(s.OtherData, r.OtherData)
FROM dbo.Products AS s
LEFT OUTER JOIN dbo.Products AS r
ON s.ProductId = r.ID;
相关问题