2015-12-04 109 views
0

我有三个表如下,映射表连接查询

student 
category 
student_category 

学生表有以下几列:

studentid, student name, studenttype 

分类表中有如下表:

categoryid, categoryname, .. 

student_category表具有以下列:

id, studentid, categoryid 

现在我有2个输入参数categoryid and studenttype所以现在我需要让它们与各自categoryid和其学生类型相关联的所有学生细节studenttype

我试过如下未给予正确的结果,

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

而且我也需要过滤的学生,其studenttype是“someinput”

我的工作PostgreSQL的。任何建议请

回答

1

您应该添加一个where子句,并使用合适的join条件。

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