2009-08-02 44 views
0

比方說,我有三個表:我如何選擇多行並將它們級聯?

table1中的字段:

memberid | name 

表2場:

interestId | interestName 

表3(用於製作成員和利益之間的關係)字段:

memberid | interestId 

現在我知道我可以用戶inner joinselect單個成員的所有興趣。

但我怎麼能級聯所有的利益在一個單一的行???

例如,我可以選擇這樣的結果:

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查詢?

在此先感謝!

+2

什麼RDBMS? SQL Server 2000,2005,MySQL等 – 2009-08-02 10:32:49

+0

SQL Server 2005 – MemoryLeak 2009-08-02 11:23:56

回答

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 
0

既然你沒有指定你的數據庫,我可以建議看看左(右)連接。

0

取決於特定的數據庫。也許它會幫助你(使用T-SQL和MS SQL Server)建立一個已知MEMBERID:

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) 
+0

非常感謝你 – MemoryLeak 2009-08-02 11:22:38

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 

這工作

相關問題