SQL Help: Select statement Concatenate a One to Many relationship

后端 未结 8 1761
天涯浪人
天涯浪人 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: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.

提交回复
热议问题