在sql server 2014中工作。想要一个问题管理应用程序。我的应用程序有下表结构。坚持 sql查询如何选择模板1的问题和答案。有没有更好的方法,请与我分享。
示例演示输入是:
GO
INSERT [dbo].[tblQuestionAns] ([QuestionAnsId], [Question], [Ans]) VALUES (1, N'Hello?', N'Hello')
GO
INSERT [dbo].[tblQuestionAns] ([QuestionAnsId], [Question], [Ans]) VALUES (2, N'World?', N'World')
GO
INSERT [dbo].[tblQuestionAns] ([QuestionAnsId], [Question], [Ans]) VALUES (3, N'Animal?', N'Animal')
GO
INSERT [dbo].[tblQuestionAns] ([QuestionAnsId], [Question], [Ans]) VALUES (4, N'Ocean?', N'Ocean')
GO
INSERT [dbo].[tblQuestionAns] ([QuestionAnsId], [Question], [Ans]) VALUES (5, N'Human?', N'Human')
GO
INSERT [dbo].[tblQuestionTemplate] ([QuestionTemplateId], [Q1], [Q2], [Q3], [Q4], [Q5]) VALUES (1, 1, 3, 5, 4, 2)
GO
INSERT [dbo].[tblQuestionTemplate] ([QuestionTemplateId], [Q1], [Q2], [Q3], [Q4], [Q5]) VALUES (2, 1, 5, 3, 2, 4)
GO
答案 0 :(得分:0)
是否需要以下结果?
SELECT tt.QuestionTemplateId,qa.Question,qa.Ans FROM tblQuestionTemplate AS tt
CROSS APPLY (VALUES(Q1),(Q2),(Q3),(Q4),(Q5)) c(Q)
INNER JOIN tblQuestionAns AS qa ON qa.QuestionAnsId=c.Q
QuestionTemplateId Question Ans 1 Hello? Hello 1 Animal? Animal 1 Human? Human 1 Ocean? Ocean 1 World? World 2 Hello? Hello 2 Human? Human 2 Animal? Animal 2 World? World 2 Ocean? Ocean