1

我有三张表 Guardian、Student 和 StudentsGuardian。表信息如下

Guardian:
 id(pk)

Student:
 id(pk)
 name
 address


StudentsGuardian:
  student_id(fk)
  guardian_id(fk)

我想选择那些 Guardian_id=2(假设)的学生。实际上这些是关系表,所以我无法想出一种方法来完成它。如果我申请加入,它将返回一个联合表,但我只需要那些具有 Guardian_id= 特定 ID 的学生的信息。
这可能是一个基本问题,但我被困在其中。谢谢

4

3 回答 3

1

使用以下查询:

SELECT s.id, s.name, s.address 
     FROM Student s 
      INNER JOIN StudentsGuardian sg ON s.id = sg.student_id 
       WHERE sg.guardian_id = 'somespecific_id'
于 2013-11-05T11:11:23.057 回答
1
SELECT 
    * 
FROM  Guardian
INNER JOIN StudentsGuardian ON StudentsGuardian.guardian_id = Guardian.id
INNER JOIN Student ON Student.id = StudentsGuardian.student_id
WHERE StudentsGuardian.guardian_id = 2
于 2013-11-05T11:10:41.547 回答
0
SELECT Student.name, Student.address 
FROM Student JOIN StudentsGuardian ON Student.id = StudentsGuardian.student_id
WHERE StudentsGuardian.guardian_id = 2

应该这样做。

于 2013-11-05T11:15:46.727 回答