自引用表 - 如何将结果作为一个表获取

时间:2013-09-17 18:21:34

标签: sql sql-server self-referencing-table

我有一张这样的表

enter image description here

请原谅他们由内部标准驱动的列名。我想知道的是如何查询这个自引用表,所以输出是这样的:

| PARENT REASON |    CHILD REASON  |
| ---------------------------------|
| Electrical    | Wire Broke       |
| Electrical    | Fuse Blown       |
| Mechanical    | Bad Gear         |
| Mechanical    | Bolt Broke       |

非常感谢您的帮助。

2 个答案:

答案 0 :(得分:1)

SELECT T2.strReason as [PARENT REASON], T1.strReason as [CHILD REASON]
FROM IngTable as T1
JOIN IngTable as T2
  ON T1.IngReasonParent = T2.IngReason

答案 1 :(得分:0)

我相信你应该把它分成一个类别表,但是......

SELECT SELECT a.strReason as [PARENT REASON], b.strReason as [CHILD REASON]
FROM table1 AS a 
INNER JOIN table1 as b ON b.lngReasonParent = a.lngReason