Vinod
Vinod

Reputation: 2311

Mapping table join query

I have three tables as follows,

student
category
student_category

Student table has the following columns:

studentid, student name, studenttype

Category table has the following tables:

categoryid, categoryname, ..

student_category table has the following columns:

id, studentid, categoryid

Now I have 2 input parameters categoryid and studenttype so now I need to get the all the student details which are associated with respective categoryid and whose student type is studenttype

I tried as follows which is not giving correct result,

SELECT
    s.*
FROM
    student s 
    JOIN 
    student_category sc ON sc.categoryid = 1;

Also I also need to filter student whose studenttype is 'someinput'

I am working on PostgreSQL. Any suggestions please

Upvotes: 0

Views: 209

Answers (1)

Vamsi Prabhala
Vamsi Prabhala

Reputation: 49260

You should add a where clause and also use the appropiate join condition.

SELECT s.*
FROM student s 
JOIN student_category sc ON sc.studentid = s.studentid
where s.studenttype = 'studenttype' --or your desired value
and sc.categoryid = 1

Upvotes: 1

Related Questions