[最优解释] select 字段ID,字段2,字段3,字段4,stuff((select ','+字段23 from table2 where table2.字段ID=tb1.字段ID for xml path('')),1,1,'') 字段23 ,stuff((select ','+字段33 from table3 where table3.字段ID=tb1.字段ID for xml path('')),1,1,'') 字段33,stuff((select ','+字段43 from table4 where table4.字段ID=tb1.字段ID for xml path('')),1,1,'') 字段43 ,stuff((select ','+字段53 from table5 where table5.字段ID=tb1.字段ID for xml path('')),1,1,'') 字段53 from table1 tb1
刚才试了 直接报错 子查询返回的值不止一个。当子查询跟随在 =、!=、<、<=、>、>= 之后,或子查询用作表达式时,这种情况是不允许的。 [其他解释] select max(字段id) from table3 where 字段33='张三' [其他解释] stuff((select ','+字段4 from table1 where table1.字段ID=tb1.字段ID for xml path('')),1,1,'') 字段4
这样只能显示一个字段 改成 stuff((select ','+字段4,字段2,字段3 from table1 where table1.字段ID=tb1.字段ID for xml path('')),1,1,'') 字段4 这样 它就将字段2和3 都显示在字段4里面了 而且 字段2和字段3的字段名称都显示进去了 [其他解释]
这样不报错了 查询结果不对 表中 字段33 为张三的有好3个 而且字段id不一样 101 102 103 三个 字段id 现在只查询出来第一个 [其他解释] select 字段ID,字段2,字段3,字段4,stuff((select ','+字段23 from table2 where table2.字段ID=tb1.字段ID for xml path('')),1,1,'') 字段23 ,stuff((select ','+字段33 from table3 where table3.字段ID=tb1.字段ID for xml path('')),1,1,'') 字段33,stuff((select ','+字段43 from table4 where table4.字段ID=tb1.字段ID for xml path('')),1,1,'') 字段43 ,stuff((select ','+字段53 from table5 where table5.字段ID=tb1.字段ID for xml path('')),1,1,'') 字段53 from table1 tb1 where tb1.字段id in (select 字段id from table3 where 字段33='张三' )