如何选择多行并级联它们?

时间:2009-08-02 10:26:04

标签: sql

假设我有三张桌子:

table1字段:

memberid | name

table2字段:

interestId | interestName

table3(用于建立成员和兴趣之间的关系)字段:

memberid | interestId 

现在我知道我可以inner join使用select一个会员的所有兴趣。

但是如何将所有兴趣级联在一行中?

例如,我可以选择此结果:

memberid   name    interstId   interestName
1         dennis   1            play basketball
1         dennis   2            music
1         dennis   3            moive

但我想得到的结果是:

memberid   name    interests
1          dennis  play basketball, music, moive

如何编写SQL查询?

提前致谢!

6 个答案:

答案 0 :(得分:1)

这取决于您使用的数据库。看看这个问题:Show a one to many relationship as 2 columns - 1 unique row (ID & comma separated list)

答案 1 :(得分:1)

在SQL Server 2005之后,您可以使用XML Path()来连接值。它似乎也非常高效。

编辑:已测试以下内容并正常工作

SELECT
    t1.memberid,
    t1.[name],
    ISNULL(STUFF(
      (
        SELECT
          ', ' + t2.interestName
          FROM 
              table2 t2
          INNER JOIN 
              table3 t3            
              ON 
              t2.interestId = t3.interestId
          WHERE 
              t3.memberid = t1.memberid
          FOR XML PATH('')
       ), 1, 2, ''
    ), 'None') As interests
FROM
    table1 t1
GROUP BY
    t1.memberid,
t1.[name]

示例代码:

DECLARE @table1 TABLE ( memberid INT IDENTITY(1,1), name VARCHAR(25) )

INSERT INTO @table1 VALUES('dennis');
INSERT INTO @table1 VALUES('mary');
INSERT INTO @table1 VALUES('bill');

DECLARE @table2 TABLE ( interestId INT IDENTITY(1,1), interestName VARCHAR(25) )

INSERT INTO @table2 VALUES('play basketball');
INSERT INTO @table2 VALUES('music');
INSERT INTO @table2 VALUES('movie');
INSERT INTO @table2 VALUES('play hockey');
INSERT INTO @table2 VALUES('wine tasting');
INSERT INTO @table2 VALUES('cheese rolling');

DECLARE @table3 TABLE ( memberid INT, interestId INT )

INSERT INTO @table3 VALUES(1,1);
INSERT INTO @table3 VALUES(1,2);
INSERT INTO @table3 VALUES(1,3);
INSERT INTO @table3 VALUES(2,2);
INSERT INTO @table3 VALUES(2,4);
INSERT INTO @table3 VALUES(2,6);
INSERT INTO @table3 VALUES(3,1);
INSERT INTO @table3 VALUES(3,5);
INSERT INTO @table3 VALUES(3,6);

    SELECT
        t1.memberid,
        t1.[name],
        ISNULL(STUFF(
          (
            SELECT
              ', ' + t2.interestName
              FROM 
                  @table2 t2
              INNER JOIN 
                  @table3 t3            
                  ON 
                  t2.interestId = t3.interestId
              WHERE 
                  t3.memberid = t1.memberid
              FOR XML PATH('')
           ), 1, 2, ''
        ), 'None') As interests
    FROM
        @table1 t1
    GROUP BY
        t1.memberid,
        t1.[name]

结果

memberid    name                      interests
----------- ----------------------------------------------------------------------- 
1           dennis                    play basketball, music, movie
2           mary                      music, play hockey, cheese rolling
3           bill                      play basketball, wine tasting, cheese rolling

答案 2 :(得分:0)

由于您没有指定数据库,我可以建议您查看左(右)连接。

答案 3 :(得分:0)

取决于特定的数据库。也许它会帮助您(使用T-SQL和MS SQL Server)获得已知的成员资格:

declare @result varchar(8000)
set @result = ''
declare @memberid int
set @memberid = 1

select @result = str(@memberid) + ' ' + (select name from table1 where memberid = @memberid) + ' '

select @result = @result + str(interestid) + ' ' + interest
from
(
select table2.interestid, table2.interestname
from table3 
inner join table2 on table2.interestid = table3.interestid
where table3.memberid = @memberid
) t1

select left(@result, LEN(@result) - 1)

答案 4 :(得分:0)

http://dev.mysql.com/doc/refman/5.0/en/group-by-functions.html#function_group-concat

GROUP_CONCAT

刚看到dbms系统上的注释,但它可以与mysql一起使用。

答案 5 :(得分:0)

SELECT    t1.memberid,    t1.name,   
 STUFF( 
             ( SELECT          ', ' + interestName          
               FROM table2 t2  
               inner join table3 as t3 
               on t2.interestId = t3.interestId and t3.memberid = t1.memberid           
               FOR XML PATH('')      //use to merge the interests
             ), 1, 2, ''    
      ) As interests
FROM    table1

这有效