SQL, questions about join

前端 未结 3 1755
长发绾君心
长发绾君心 2021-01-23 04:48

I have two tables in sql 2012: name and prod with structure:

name: id int increment, name1 nvarchar(50)

prod:

3条回答
  •  北海茫月
    2021-01-23 04:57

    sqlfiddle

    select
    n.nameid [id],
    n.name [name],
    count(*)[count],
        stuff(
            (
             select ', ' + p.prod
             from prodtbl as p
             where p.nameid = n.nameid
             for xml path(''),  type).value('.', 'nvarchar(max)'), 1, 1, '') as products
    from nametbl n, prodtbl p
    where p.nameid = n.nameid
    group by n.nameid, n.name
    order by [id];
    

    enter image description here

提交回复
热议问题