gpt4 book ai didi

sql - 从函数 PostgreSQL 而不是记录返回多列和多行

转载 作者:行者123 更新时间:2023-11-29 11:31:19 25 4
gpt4 key购买 nike

我正在网上阅读有关 PostgreSQL 上的函数并返回结果在这个链接中:

  1. SQL function return-type: TABLE vs SETOF records
  2. How do I reference named parameters in Postgres sql functions?
  3. http://www.postgresqltutorial.com/plpgsql-function-returns-a-table/

我写了这个函数:

create or replace function brand_hierarchy(account_value int)
RETURNS table (topID INTEGER, accountId INTEGER, liveRowCount bigint,archiveRowCount bigint)
AS
$BODY$
SELECT * FROM my_client_numbers
where accountId = coalesce($1,accountId);
$BODY$
LANGUAGE sql;

哪个有效并在单列记录类型中返回结果。请注意,可能会返回不止一行。

现在的响应是:

record
(1172,1172,1011,0)
(1172,1412,10,40)
.....

我希望我的结果不是作为记录而是作为多列

|---------|---------|------------|----------------|
| topID |accountId|liveRowCount|archiveRowCount |
|---------|---------|------------|----------------|
| 1172 |1172 | 1011 | 0 |
| 1172 |1412 | 10 | 40 |

有没有办法从 PostgreSQL 函数返回多列

最佳答案

应在 FROM 子句中使用返回表(或 setof)的函数:

select * 
from brand_hierarchy(1234)

关于sql - 从函数 PostgreSQL 而不是记录返回多列和多行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49299858/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com