映射表连接查询

时间:2015-12-04 18:09:21

标签: sql postgresql

我有三张表如下,

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 个答案:

答案 0 :(得分: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
相关问题