日期:2014-05-18  浏览次数:20580 次

一个超简单的Sql求解
SubCode SbuName
  001 x
  002 x
由于SbuName要绑定在下拉框上,所以

要求把SbuName x查询成一行,显示SubCode是为两行。

------解决方案--------------------
http://topic.csdn.net/u/20080612/22/c850499f-bce3-4877-82d5-af2357857872.html
不写了
------解决方案--------------------
SQL code
--> 测试数据:[tb]
IF OBJECT_ID('[tb]') IS NOT NULL DROP TABLE [tb]
GO 
CREATE TABLE [tb]([SubCode] VARCHAR(3),[SbuName] VARCHAR(1))
INSERT [tb]
SELECT '001','x' UNION ALL
SELECT '002','x'
--------------开始查询--------------------------

SELECT DISTINCT SubCode =STUFF((SELECT ','+[SubCode] FROM [tb] WHERE [SbuName]=t.[SbuName] FOR XML PATH('')),1,1,''),[SbuName]
FROM [tb] AS t 
----------------结果----------------------------
/* 
SubCode        SbuName
-------------------------
001,002         x

*/

------解决方案--------------------
探讨

select distinct SubDepartment1Code,'all' as SubDepartment1Name from tblEmployee where SubDepartment1Code !='' and SubDepartment1Name is not null
union all
select distinct SubDepartment1Code,SubD……