sql查询获取标题名称

时间:2014-01-13 13:06:11

标签: sql-server tsql

我坚持使用sql查询(SQL Server 2012),我有一个名为Function的表,它包含functionId和Title以及第二个表Function_Hierarchy,将function_ids作为父子关系的组合。我想针对每个functionID打印标题....

enter image description here

enter image description here

这是我的SQL查询,

SELECT B.Parent_Function_ID, A.Title AS Parent_Function, B.Child_Function_ID, A.Title AS   Child_Function
FROM [dbo].Functions AS A, [dbo].[Function_Hierarchy] AS B
WHERE B.Parent_Function_ID =A.Function_ID

1 个答案:

答案 0 :(得分:1)

嗨试试这个............

SELECT 
B.Parent_Function_ID,
 Parent_Function = (select top 1 A.Title from [dbo].Functions a  where  a.FunctionId = b.Parent_Function_id),
 B.Child_Function_ID,
   Child_Function = (select top 1 C.Title from [dbo].Functions c  where  c.FunctionId = b.Child_Function_id)
FROM [dbo].[Function_Hierarchy] AS B