简体   繁体   中英

postgresql query

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.

select dfunction(t1date) from Table1 order by t1date asc 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)
)

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;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM