【问题标题】:How to retrieve the student information from the table?如何从表中检索学生信息?
【发布时间】:2016-08-31 03:08:20
【问题描述】:
student table   teacher table    sports table     parents table
---------     --------------     ------------      ---------------
id  name        id  name        id  name           id    stud_id  fathername   mothername
------------   ------------    ------------        -----------------------------------------
1    S1           1     T1         1    SP1         1       1       xxxxxx       yyyyyyy
2    S2           2     T2         2    SP2         2      2        abc          aaa    
3    S3           3     T3         3    SP3 

student_teacher table               student_sports table
id    stud_id     teacher_id       id      sutd_id    sports_id
------------------------------     ------------------------------
1      1            1               1        1          1
2      1            2               2        1          2
3      1            3               3        1          3
4      2            2               4        3          2
5      2            3               5        3          3

如何编写查询以从所有表中获取学生 S1 的信息。 例如,学生 S1 参加的运动名称、教授学生 S1 的老师姓名、学生 S1 家长信息。这里学生、教师、父表中的 id 是主键。 stud_id、teacher_id、sports_id 是外键,指的是学生、教师、运动表的主键。 请帮我从表中获取学生 S1 的记录。提前致谢。

【问题讨论】:

    标签: mysql sql join


    【解决方案1】:

    你可以简单地使用 where 子句:

    select S.name, T.name, SS.name, P.fathername, P.mothername 
    from student S, teacher T, sports SS, parents P,student_teacher ST, student_sports SSP 
    where S.id = ST.stud_id and T.id = ST.teacher_id and
    S.id = SSP.stud_id and SS.id = SSP.sports_id and
    S.id = P.stud_id
    

    【讨论】:

      【解决方案2】:

      解决方法如下:

      select st.name as Student, t.name as teacher, sp.name as sports, p.fathername,p.mothername from student st,teacher t,sports sp,parents p,student_teacher s_t,student_sports s_s where s_t.stud_id=st.id and s_t.teacher_id=t.id and p.stud_id=st.id and s_s.stud_id=st.id;
      

      【讨论】:

        【解决方案3】:

        您需要合并student_teacherstudent_sports 表。没有办法让哪位老师教哪位学生参加一项特定的运动。我们只知道学生 1 参加了运动 1、2 和 3,并由老师 1、2 和 3 教授。我们不知道哪个老师参加了哪个运动。你应该有这个:

        student_sports_teacher table
        +----+---------+-----------+------------+
        | id | stud_id | sports_id | teacher_id |
        +----+---------+-----------+------------+
        |  1 |       1 |         1 |          1 |
        |  2 |       1 |         2 |          2 |
        |  3 |       1 |         3 |          3 |
        |  4 |       2 |         1 |          2 |
        |  5 |       2 |         2 |          3 |
        +----+---------+-----------+------------+
        

        完成后,您可以使用连接来获取所有数据:

        SELECT * FROM student s
        LEFT JOIN parents p ON s.id = p.stud_id
        LEFT JOIN student_sports_teacher sst ON sst.stud_id = s.id
        LEFT JOIN sports sp ON sp.id = sst.sports_id
        LEFT JOIN teacher t ON t.id = sst.teacher_id
        

        【讨论】:

        • 不需要使用左连接。 OP 正在使用外键,因此您不会有任何孤立记录。
        • 只是一个习惯,我总是指定join类型。
        【解决方案4】:

        你可以使用内连接

             select st.name as Student, t.name as teacher, sp.name as sports, 
                  p.fathername,p.mothername 
             from student st
             Inner join student_teacher as st on st s_t.stud_id=st.id 
             Inner join teacher t on s_t.teacher_id=t.id 
             Inner join parents p on p.stud_id=st.id 
             Inner join student_sports ss on  ss.stud_id=st.id;
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2019-12-09
          • 1970-01-01
          • 1970-01-01
          • 2017-10-25
          • 2014-07-04
          • 1970-01-01
          • 2012-07-18
          • 2015-01-25
          相关资源
          最近更新 更多