Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/72.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql 选择记录作为逗号分隔的字符串_Sql_Sql Server_Tsql_Select_Sql Server 2008 R2 - Fatal编程技术网

Sql 选择记录作为逗号分隔的字符串

Sql 选择记录作为逗号分隔的字符串,sql,sql-server,tsql,select,sql-server-2008-r2,Sql,Sql Server,Tsql,Select,Sql Server 2008 R2,因此,我有以下表格: Table 1 fname mi lname empid John A Smith 1202 Bob Doe 9815 Table 2 unid empid 1015 1202 1015 9815 Table 3 unid Item 1015 ABC 1015 DEF 我的预期输出应为(当提供unid=1015时) 现在这将是理想的,但我非常

因此,我有以下表格:

Table 1

fname    mi    lname    empid

John     A     Smith    1202
Bob            Doe      9815


Table 2

unid    empid

1015     1202
1015     9815


Table 3

unid    Item

1015    ABC
1015    DEF
我的预期输出应为(当提供unid=1015时)

现在这将是理想的,但我非常乐意处理前端重复的[Item]值

我目前的发言是:

select p.FNAME,p.MI,p.LNAME, ac.EQUIP from table1 t1, table2 t2, table3 t3  
where t1.EMPID = t2.EMPID and t2.UNID = t3.UNID and t2.unid = '1015' group by t1.FNAME, t1.MI, 
t1.LNAME,t3.EQUIP
在我的一生中,我无法理解如何将项中的值(可以是0或更多,最多8个)作为一个逗号分隔的字符串。我的问题是,由于站点/客户端的限制,我无法使用SP,但这必须在一条SQL语句中完成


这是在SQLServer2008R2上

你必须使用。这是什么黑魔法?谢谢你给我介绍了一件我从未见过的东西。1个快速问题因为我很难让它按原样工作,在这种情况下stuff子句需要在逗号之后吗?只需在t.lname之后加逗号,并确保提供正确的名称标识符作为快速跟进,我到底该如何去掉后面的逗号?这是遗留软件,我的能力非常有限
Select distinct t.fname,t.mi,t.lname,
    STUFF((Select distinct i.item +  ',' 
        from Table3 i 
    where t.unid = tt.unid AND i.unid = '1015' 
    ORDER BY i.unid
    FOR XML PATH(''),TYPE).value('.', 'NVARCHAR(MAX)') 
                        , 1,  0, ' ') from Table1 t
                        INNER JOIN Table2 tt
                        ON tt.empid = t.empid
                     group by
                            t.FNAME, 
                            t.MI,
                            t.LNAME
Select distinct t.fname,t.mi,t.lname,
    STUFF((Select distinct i.item +  ',' 
        from Table3 i 
    where t.unid = tt.unid AND i.unid = '1015' 
    ORDER BY i.unid
    FOR XML PATH(''),TYPE).value('.', 'NVARCHAR(MAX)') 
                        , 1,  0, ' ') from Table1 t
                        INNER JOIN Table2 tt
                        ON tt.empid = t.empid
                     group by
                            t.FNAME, 
                            t.MI,
                            t.LNAME