SQL Help: Select statement Concatenate a One to Many relationship

后端 未结 8 1686
天涯浪人
天涯浪人 2021-01-03 16:28

For example I have two tables. The first table is student while the second table are the courses that the a student is taking. How can I use a select statement so that I can

相关标签:
8条回答
  • 2021-01-03 17:01

    It depends on which server you're using. SQL Server? MySQL? Other?

    0 讨论(0)
  • 2021-01-03 17:02
    create table Project (ProjectId int, Description varchar(50));
    insert into Project values (1, 'Chase tail, change directions');
    insert into Project values (2, 'ping-pong ball in clothes dryer');
    
    create table ProjectResource (ProjectId int, ResourceId int, Name varchar(15));
    insert into ProjectResource values (1, 1, 'Adam');
    insert into ProjectResource values (1, 2, 'Kerry');
    insert into ProjectResource values (1, 3, 'Tom');
    insert into ProjectResource values (2, 4, 'David');
    insert into ProjectResource values (2, 5, 'Jeff');
    
    SELECT *, 
      (SELECT Name + ' ' AS [text()] 
       FROM ProjectResource pr 
       WHERE pr.ProjectId = p.ProjectId 
       FOR XML PATH ('')) 
    AS ResourceList 
    FROM Project p
    
    --    ProjectId    Description                        ResourceList
    --    1            Chase tail, change directions      Adam Kerry Tom 
    --    2            ping-pong ball in clothes dryer    David Jeff 
    
    0 讨论(0)
  • 2021-01-03 17:08

    Generally, what you're talking about is a join:

    SELECT 
        S.*,
        SC.*
      FROM
        Students S
        INNER JOIN Student_Courses SC
          ON S.student_id = SC.student_id
    

    However, that will give you one row per course. SQL doesn't make it easy to get the set of courses as a comma delimited list in a single row (that's not a set-based operation). Depending on the vendor, there are different ways to do it, involving looping.

    0 讨论(0)
  • 2021-01-03 17:08

    Are you using 2005 or 2008? If so, then lookup the PIVOT comand. It should be more efficient than a function with a cursor.

    0 讨论(0)
  • 2021-01-03 17:12

    So you want to see:

    'Jade', 'Math, English, History'
    'Kieveli', 'History, Biology, Physics'
    

    Yes, comma separated is always desirable, but SQL isn't good at it. Here's the approach I was always planning on using:

    Create a Function on the SQL server that uses a cursor to loop over the subquery (sorry - I didn't fully test this):

    CREATE FUNCTION commacourselist(@studentname varchar(100))
    RETURNS @List varchar(4096)
    AS
    BEGIN
    
    DECLARE @coursename varchar(100)
    
    DECLARE FOR
    SELECT course.name FROM course WHERE course.studentname = @studentname
    OPEN coursecursor
    
    FETCH NEXT FROM coursecursor INTO @coursename 
    WHILE @@FETCH_STATUS = 0
    BEGIN
     IF @List = ''
     BEGIN
      SET @List = @coursename
     END
     ELSE
     BEGIN
      SET @List = @List + ',' + @coursename 
     END
     FETCH NEXT FROM coursecursor INTO @coursename 
    END
    CLOSE coursecursor 
    DEALLOCATE coursecursor 
    
    RETURN
    END
    GO
    

    Then call the function in the query:

    SELECT student.name, 
           commacourselist( student.name )  
    FROM student
    
    0 讨论(0)
  • 2021-01-03 17:14

    I think this MySQL page will help you with that. http://dev.mysql.com/doc/refman/4.1/en/group-by-modifiers.html

    0 讨论(0)
提交回复
热议问题