如何在PostgreSQL中创建具有内部连接和变量的视图?

如何在PostgreSQL中创建具有内部连接和变量的视图?,sql,postgresql,view,Sql,Postgresql,View,我正在使用PostgreSQL数据库。我需要创建一个视图,该视图由一个查询和多个表上的连接组成,但我在某一点上卡住了。请阅读下表定义和我创建的查询 Tables: Students --------- id -> UUID full_name -> VARCHAR email_id -> VARCHAR location -> VARCHAR created_at -> timestamp modified_at -> timestamp Subjects

我正在使用PostgreSQL数据库。我需要创建一个视图,该视图由一个查询和多个表上的连接组成,但我在某一点上卡住了。请阅读下表定义和我创建的查询

Tables:

Students
---------
id -> UUID
full_name -> VARCHAR
email_id -> VARCHAR
location -> VARCHAR
created_at -> timestamp
modified_at -> timestamp

Subjects
--------
id -> UUID
title -> VARCHAR
classroom -> VARCHAR
created_at -> TIMESTAMP


Applications
------------
id -> UUID
student_id -> FOREIGN KEY (Students.id)
subject_id -> FOREIGN KEY (Subjects.id)
verified -> BOOLEAN
application_response -> JSON
status_id -> FOREIGN KEY (ApplicationStatus.id)
created_at -> TIMESTAMP

ApplicationStatus
-----------------
id -> UUID
application_status -> VARCHAR
category -> VARCHAR

Scores
-------
student_id -> FOREIGN KEY (Students.id)
subject_id -> FOREIGN KEY (Subjects.id)
score -> NUMERIC
以下是我创建的SQL查询:

create or replace view testing_list_view as 
select c.id as student_id,
a.subject_id as subject_uid,
c.full_name, 
c.email_id,  
c.created_at, 
p.score, 
s.application_status,
a.verified,
a.application_response
from students c
inner join scores p
on p.student_id=c.id and p.subject_id = 'ff342ada-f5gb-44fb-bdth-44e3n59f5448'
inner join applications a
on a.student_id = c.id and a.subject_id= 'ff342ada-f5gb-44fb-bdth-44e3n59f5448'
inner join applicationstatus s 
on s.id = a.status_id
where c.id in 
(select student_id from applications where subject_id='ff342ada-f5gb-44fb-bdth-44e3n59f5448')
在这里,我得到了给定
科目id
的学生名单,以及分数、申请状态和一些我需要加入的其他字段

我的要求是为这个查询创建一个视图,这样我就只把
主题id
传递给视图(
从测试列表视图中选择*,其中主题uid='ff342ada-f5gb-44fb-bdth-44e3n59f5448'
),然后我就会得到一个申请给定主题的学生列表。但是,我被困在这里,就像上面的join查询一样,
subject\u id
在join子句中多次是必需的,并且是硬编码的。因此,我无法将其作为一种观点

我在想,如果存在某种变量,在join子句中,我将在required子句中使用该变量,在查询视图时,我将向其传递值(subject_id)


如果需要更多说明,请告诉我。

您应该定义视图,而不使用
WHERE
子句以及
主题id的所有其他限制:

create or replace view testing_list_view as 
select c.id as student_id,
       a.subject_id as subject_uid,
       c.full_name, 
       c.email_id,  
       c.created_at, 
       p.score, 
       s.application_status,
       a.verified,
       a.application_response
from students c
   inner join scores p
      on p.student_id=c.id
   inner join applications a
      on a.student_id = c.id and a.subject_id = p.subject_id
   inner join applicationstatus s 
      on s.id = a.status_id;
您可以在使用视图时指定条件,例如

SELECT * FROM testing_list_view
WHERE subject_uid = 'ff342ada-f5gb-44fb-bdth-44e3n59f5448';

视图不能有参数,但您可以创建一个SQL函数(
返回表(…)
),该函数将参数作为输入,并在此处返回查询确定的结果-@JosMac看起来与我的问题相关。让我试试那个问题的答案。谢谢你的回答。我不明白答案。你能详细说明一下吗?我通过添加我预想的视图定义进行了详细说明。当我运行你给出的查询时,我得到了一个错误-SQL错误[42702]:错误:列引用“subject_id”不明确。这是因为subject_id列在应用程序和分数表中都是外键。这就是它给出错误的原因。哪个查询?视图定义中的列
subject\u id
是限定的,不能含糊不清。我的查询中有一个错误,这就是出现错误的原因。那么,查询成功运行了。但是,结果并不正确。因为,当我运行我的原始查询(在问题中)时,它给出了215个计数(这是正确的)。但是,当我创建一个没有subject\u id的视图时,我使用了subject\u id,结果计数为2150。