本文介绍了我尝试了我的查询但在sql查询中显示错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 在数据库记录中如下 Schdate课程课程学院 8月MFA 1 AD 28 AUg EFA 2 AD 8月28日MC 3 AD i想要输出如下 28Aug(MFA -S1,EFA -S2,MC -S3) 获得以上输出我尝试了如下查询 SELECT Facuty, STUFF(( SELECT ' (' + 转换( char ( 7 ),[Schdate], 100 )+ ' ' + [课程] + ' - S' + [Session] + ' )' 来自 Tb_Sch_Time_Table_Details sch 其中 sch.Facuty = sch1.Facuty FOR XML PATH (' ')[course]) FROM Tb_Sch_Time_Table_Details sch1 GROUP BY Facuty ORDER BY 1 当我执行上述查询时显示如下错误 语法不正确'课程'附近 请帮助我如何解决问题以获得正确的结果。解决方案 只需尝试以下查询并让我知道结果。 选择 转换( char ( 7 ),sch1。[Schdate] , 100 )+ ' (' + STUFF((选择 ' ,' + ltrim(rtrim([Course]))+ ' - S' + ltrim(rtrim ([会话]))来自 Tb_Sch_Time_Table_Details sch 其中 sch.Schdate = sch1.Schdate FOR XML PATH(' ')), 1 , 1 , ' ')+ ' )' 来自 Tb_Sch_Time_Table_Details sch1 group by Schdate order by 1 尝试这个 SELECT Faculty,(SELECT'('+ Convert(char(7),[Schdate],100)+ ''+ [课程] +' - S'+演员([会话]为varchar(5))+')'来自@TBL sch 其中sch.Faculty = sch1.Faculty FOR XML PATH('')) FROM @TBL sch1 GROUP BY Faculty ORDER BY 1 In database records as follows Schdate Course Session Faculty 28 Aug MFA 1 AD 28 AUg EFA 2 AD 28 Aug MC 3 AD i want the output as follows 28Aug(MFA -S1,EFA -S2,MC -S3)for getting a above output i tried my query as followsSELECT Facuty, STUFF((SELECT '( ' + Convert(char(7),[Schdate],100 )+ ' ' + [Course] + '-S' + [Session] + ')' from Tb_Sch_Time_Table_Details sch where sch.Facuty = sch1.Facuty FOR XML PATH('') [course]) FROM Tb_Sch_Time_Table_Details sch1GROUP BY FacutyORDER BY 1When i execute the above query shows error as followsIncorrect syntax near 'Course'please help me how can i solve the problem to get the correct ouptut. 解决方案 Just try the below query and let me know the result.select Convert(char(7),sch1.[Schdate],100 )+'('+STUFF((select ','+ ltrim(rtrim([Course])) +'-S'+ltrim(rtrim([Session])) from Tb_Sch_Time_Table_Details schwhere sch.Schdate = sch1.SchdateFOR XML PATH('')),1,1,'')+')' from Tb_Sch_Time_Table_Details sch1group by Schdateorder by 1Try thisSELECT Faculty, (SELECT '( ' + Convert(char(7),[Schdate],100 )+ ' ' + [Course] + '-S' + cast([Session] as varchar(5) )+ ')' from @TBL sch where sch.Faculty = sch1.Faculty FOR XML PATH('') ) FROM @TBL sch1GROUP BY FacultyORDER BY 1 这篇关于我尝试了我的查询但在sql查询中显示错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 07-17 13:56