【问题标题】:How can i select multiple row and cascade them?如何选择多行并将它们级联?
【发布时间】:2010-11-16 03:49:30
【问题描述】:

假设我有三个表:

table1 字段:

memberid | name

table2 字段:

interestId | interestName

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

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

提前致谢!

【问题讨论】:

  • 什么关系型数据库? SQL Server 2000、2005、MySQL 等

标签: sql


【解决方案1】:

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

【讨论】:

    【解决方案2】:

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

    【讨论】:

      【解决方案3】:

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

      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】:

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

        GROUP_CONCAT
        

        刚刚看到关于dbms系统的评论,但是mysql可以用。

        【讨论】:

          【解决方案5】:

          从 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
          

          【讨论】:

          • 列 't3.interestId' 在选择列表中无效,因为它不包含在聚合函数或 GROUP BY 子句中。怎么了?
          • 嗨丹尼斯,现在已经修复了代码并提供了一个工作示例:)
          【解决方案6】:
          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
          

          这行得通

          【讨论】:

            猜你喜欢
            • 2013-09-06
            • 2021-05-30
            • 2019-04-21
            • 1970-01-01
            • 2012-05-13
            • 2011-02-22
            • 2018-09-22
            • 2017-06-16
            • 1970-01-01
            相关资源
            最近更新 更多