如何用函数返回两列 [英] How to return two columns with function

查看:122
本文介绍了如何用函数返回两列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用我的SQL函数返回2个值:

I want to return 2 values with my SQL function:

CREATE OR REPLACE FUNCTION get_avg_prices(...)
  RETURNS table(avg_sale_price decimal, avg_rent_price decimal) 
AS
$$

SELECT 
  building_prices.avg_sale_price, 
  building_prices.avg_rent_price
FROM 
  ...

$$
  LANGUAGE sql;

它可以工作,但返回单列中的值:

It works but returns the values in single column:

如何返回值

推荐答案

表函数(定义为函数返回表或返回setof )需要像表一样在 from 子句中使用。

Table functions (functions defined as returns table or returns setof) need to be used in the from clause like a table.

因此您需要使用:

select * 
from get_avg_prices(...);

仅应将标量函数(仅返回单个值(例如数字)的函数)放入选择列表。

Only scalar functions (functions which return only a single value, e.g. a number) should be put into the select list.

这篇关于如何用函数返回两列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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