0

I have a database table like that

Table student
_______________
id_student int (PK)
student_name VARCHAR

and I have a recursive loop: A student can oversee many students, and a student can be overseen by many students so a new table:

Table oversee
________________
id_student pk, fk 
id_overseen pk, fk
date date

the problem is that I want to get the list that I have I made an sql query:

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;

the problem is that's the query returns the wrong answers I mean if we have two lines in the table, it will return 4 lines. I want to get every student with his overseen: Student | overseen. Someone has any idea please? Thanks.

10
  • It is unclear what you want query to return. You sure didn't write any recursive logic anywhere. Commented Jul 10, 2018 at 14:43
  • SQL is a language but different databases implement parts of the language differently, so what database are you using (re-tag your question appropriately). Commented Jul 10, 2018 at 14:45
  • @Andreas I want to get a table with student | overseen | date Commented Jul 10, 2018 at 14:45
  • 1
    You should provide sample data and the expected results along with the query you've already provided. You query has a cross product (theta join) between sr1 and sr2. Commented Jul 10, 2018 at 14:47
  • 1
    Don't clarify question in comments. Edit the question to clarify it. Delete the comment. Commented Jul 10, 2018 at 14:50

1 Answer 1

2

I want to get a table with 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
Sign up to request clarification or add additional context in comments.

2 Comments

I don't think it's working, that returns every student in the database
@Devweb If you don't want the students that are not overseen, remove the two LEFT keywords.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.