我有一个这样的数据库表
Table student
_______________
id_student int (PK)
student_name VARCHAR
,我有一个递归循环: 一个学生可以监督许多学生,而一个学生可以被许多学生监督 所以有一张新桌子:
Table oversee
________________
id_student pk, fk
id_overseen pk, fk
date date
问题是我想获取我拥有的列表 我做了一个SQL查询:
with
sr1 as ( select s.student_name as over from student s, oversee o where o.id_student = s.id_student),
sr2 as (select s.student_name as overseen from student s, oversee o where o.id_overseen = s.id_student)
select distinct * from sr1, sr2;
问题是查询返回错误的答案 我的意思是,如果表中有两行,它将返回4行。 我想让每个学生都受到他的监督: 学生|监督。 有人有什么主意吗? 谢谢。
答案 0 :(得分:2)
我想用
来获得一张桌子student | overseen | date
SELECT s.student_name AS student
, s2.student_name AS overseen
, oversee.date
FROM student s
JOIN oversee ON oversee.id_student = s.id_student
JOIN student s2 ON s2.id_student = oversee.id_overseen