gpt4 book ai didi

带有两个选择语句的 PostgreSQLFunction

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

我正在尝试创建一个 PostgreSQL 函数,声明两个变量以在每个变量上返回不同查询的结果,查询本身非常简单,并且依赖于具有不同条件的相同选择:

select count (*) from editions where year >= '2000'

select count (*) from editions where year < '2000'

表的结构也很简单,一共有三行:

  1. 代码(数字)
  2. 版本(字符)
  3. 年份(整数)

如何创建此函数?

最佳答案

这太基础了。我想你可以在 Google 中找到很多教程来创建这个 Function。你的 Year 列是 Integer 所以不要像 String 一样使用 Single Quote..

函数 totalRecords1()

CREATE OR REPLACE FUNCTION totalRecords1()
RETURNS integer AS $total1$
declare
total1 integer;
BEGIN
SELECT count(*) into total1
FROM your_table
WHERE year >= 2000;
RETURN total1;
END;
$total1$ LANGUAGE plpgsql;

函数 totalRecords2()

CREATE OR REPLACE FUNCTION totalRecords2()
RETURNS integer AS $total2$
declare
total2 integer;
BEGIN
SELECT count(*) into total2
FROM your_table
WHERE year < 2000;
RETURN total2;
END;
$total2$ LANGUAGE plpgsql;

选择语句

select totalrecords1(), totalrecords2();

您可以在DEMO 中看到这里

关于带有两个选择语句的 PostgreSQLFunction,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53421737/

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