PostgreSQL 查询

发布于 2024-11-09 10:00:58 字数 475 浏览 0 评论 0原文

我有一个包含一列 (t1date) 的表,如下所示:

Table1

t1date  
----------  
2011-05-24

还有一个采用一个参数 dfunction(fdate) 的函数。我想将 t1date 的值传递给函数。类似于:

SELECT * 
  FROM dfunction(SELECT t1date 
                   FROM Table1 
               ORDER BY t1date 
                  LIMIT 1)

这相当于

SELECT * 
  FROM dfunction('2011-05-24')

我不想为此编写另一个函数。我正在寻找的是使用 select 语句。是否可以?如果是这样怎么办。

I have a table with one column (t1date) as:

Table1

t1date  
----------  
2011-05-24

There is also a function which takes one parameter dfunction(fdate). I want to pass the value of the t1date to the function. Something like:

SELECT * 
  FROM dfunction(SELECT t1date 
                   FROM Table1 
               ORDER BY t1date 
                  LIMIT 1)

which is equivalent to

SELECT * 
  FROM dfunction('2011-05-24')

I don't want to write another function for this. What I'm looking for is to use the select statement. Is it possible? If so how.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

绝對不後悔。 2024-11-16 10:00:58
select dfunction(t1date) from Table1 order by t1date asc limit 1;
select dfunction(t1date) from Table1 order by t1date asc limit 1;
挽清梦 2024-11-16 10:00:58

据我了解,子查询用作标量表达式时,应括在括号中。那么,这个怎么样:

SELECT * 
FROM dfunction(
   (SELECT t1date 
      FROM Table1 
  ORDER BY t1date 
     LIMIT 1)
)

I understand, a subquery, when used as a scalar expression, should be enclosed in parentheses. So, how about this:

SELECT * 
FROM dfunction(
   (SELECT t1date 
      FROM Table1 
  ORDER BY t1date 
     LIMIT 1)
)
千紇 2024-11-16 10:00:58

我很难准确理解你想要什么。我有几个选择,如果这些都不是您想要的,也许您可​​以解释更多?

create table foo1 ( t1 date, id int );
insert into foo1 values ('2001-01-01', 1), ('2002-01-01', 2),  ('2003-01-01', 3),  ('2004-01-01', 4);

create or replace function foo1(date) returns setof foo1 as $ select * from foo1 where $1 > t1 order by t1 asc; $ language 'sql';
select * from foo1('2002-02-02');

create or replace function foo2(text) RETURNS SETOF foo1 as $
DECLARE
 q alias for $1;
 r foo1%rowtype;
BEGIN
 for r in execute q loop
  return next r;
 end loop;
 return;
END;
$ language 'plpgsql';
select * from foo2('select * from foo1 order by t1 asc limit 1');
drop table foo1 cascade;

I'm having difficulty understanding exactly what you want. I have a few options, perhaps you can explain more if neither of these are what you want?

create table foo1 ( t1 date, id int );
insert into foo1 values ('2001-01-01', 1), ('2002-01-01', 2),  ('2003-01-01', 3),  ('2004-01-01', 4);

create or replace function foo1(date) returns setof foo1 as $ select * from foo1 where $1 > t1 order by t1 asc; $ language 'sql';
select * from foo1('2002-02-02');

create or replace function foo2(text) RETURNS SETOF foo1 as $
DECLARE
 q alias for $1;
 r foo1%rowtype;
BEGIN
 for r in execute q loop
  return next r;
 end loop;
 return;
END;
$ language 'plpgsql';
select * from foo2('select * from foo1 order by t1 asc limit 1');
drop table foo1 cascade;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文