传入“WHERE"PostgreSQL 视图的参数? [英] Pass In "WHERE" parameters to PostgreSQL View?

查看:29
本文介绍了传入“WHERE"PostgreSQL 视图的参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我通过一系列嵌套子查询对跨越 4 个表的 PostgreSQL 数据库进行了相当复杂的查询.然而,尽管外观和设置看起来有点棘手,但最终它会根据两个外部参数的匹配(两个字符串需要与不同表中的字段匹配)返回两列(来自同一个表,如果这对情况有帮助的话).我对 PostgreSQL 中的数据库设计还很陌生,所以我知道存在一种名为 Views 的看似神奇的东西,这似乎可以帮助我,但也许不会.

I have a rather complicated query on my PostgreSQL database spanning 4 tables via a series of nested subqueries. However, despite the slightly tricky looking appearance and setup, ultimately it will return two columns (from the same table, if that helps the situation) based on that matching of two external parameters (two strings need to match with fields in different tables). I'm fairly new to database design in PostgreSQL, so I know that this seemingly magical thing called Views exist, and that seems like it could help me here, but perhaps not.

有什么方法可以在视图中移动我的复杂查询,然后以某种方式将我需要匹配的两个值传递给它?这将大大简化我的前端代码(通过将复杂性转移到数据库结构).我可以创建一个包含我的静态示例查询的视图,它工作得很好,但是只适用于一对字符串值.我需要能够将它用于各种不同的值.

Is there some way I can move my complex query inside a view and somehow just pass it the two values I need to match? That would greatly simplify my code on the front-end (by shifting the complexities to the database structure). I can create a view that wraps my static example query, and that works just fine, however that only works for one pair of string values. I need to be able to use it with a variety of different values.

因此我的问题是:是否可以将参数传递到静态视图中并使其变为动态"?或者,视图可能不是处理它的正确方法.如果有其他更好的方法,我会全力以赴!

Thus my question is: is it possible to pass parameters into an otherwise static View and have it become "dynamic"? Or perhaps a View is not the right way to approach it. If there's something else that would work better, I'm all ears!

**根据评论中的要求,这是我现在的查询:

* * As requested in comments, here's my query as it stands now:

SELECT   param_label, param_graphics_label
  FROM   parameters
 WHERE   param_id IN 
         (SELECT param_id 
            FROM parameter_links
           WHERE region_id = 
                 (SELECT region_id
                    FROM regions
                   WHERE region_label = '%PARAMETER 1%' AND model_id =
                         (SELECT model_id FROM models WHERE model_label = '%PARAMETER 2%')
                 )
         ) AND active = 'TRUE'
ORDER BY param_graphics_label;

参数由上面的百分号分隔.

Parameters are set off by percent symbols above.

推荐答案

你可以使用集合返回函数:

You could use a set returning function:

create or replace function label_params(parm1 text, parm2 text)
  returns table (param_label text, param_graphics_label text)
as
$body$
  select ...
  WHERE region_label = $1 
     AND model_id = (SELECT model_id FROM models WHERE model_label = $2)
  ....
$body$
language sql;

然后你可以这样做:

select *
from label_params('foo', 'bar')

顺便说一句:你确定你想要:

Btw: are you sure you want:

AND model_id = (SELECT model_id FROM models WHERE model_label = $2)

如果 model_label 不是唯一的(或主键),那么这最终会引发错误.你可能想要:

if model_label is not unique (or the primary key) then this will throw an error eventually. You probably want:

AND model_id IN (SELECT model_id FROM models WHERE model_label = $2)

这篇关于传入“WHERE"PostgreSQL 视图的参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆