PostgreSQL 存储过程

发布于 2024-12-03 17:11:17 字数 144 浏览 0 评论 0原文

我有两个独立的 PostgreSQL 计数查询,我想将它们输出为一份合并报告。我对此做了一些研究,发现它可以通过存储过程来完成,但我不确定应该如何去做(我对 Postgres 编程相当陌生)。

两个查询都返回计数。

任何对此的见解将不胜感激!

I have two separate PostgreSQL count queries that I would like to output as one combined report. I have done a little bit of research into this and found that it could be done through a stored procedure, but I am not sure how I should go about doing this (I'm fairly new to Postgres programming).

Both of the queries are returning counts.

Any insight into this would be much appreciated!

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

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

发布评论

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

评论(2

弄潮 2024-12-10 17:11:17

您甚至不需要为此存储过程。您可以只进行一个大查询:

SELECT a.a_count, b.b_count FROM
  (SELECT COUNT(*) a_count FROM table_a) AS a,
  (SELECT COUNT(*) b_count FROM table_b) AS b;

You don't even need a stored procedure for this. You can just make one big query:

SELECT a.a_count, b.b_count FROM
  (SELECT COUNT(*) a_count FROM table_a) AS a,
  (SELECT COUNT(*) b_count FROM table_b) AS b;
走过海棠暮 2024-12-10 17:11:17

我不相信 PostgreSQL 有存储过程,只有函数。但是,您可以使用函数来完成您正在谈论的事情。

CREATE FUNCTION getQtyOrders(customerID int) RETURNS int AS $
DECLARE
qty int;
BEGIN
SELECT COUNT(*) INTO qty
FROM Orders
WHERE accnum = customerID;
RETURN qty;
END;

I don't believe PostgreSQL has stored procedures, only functions. However, you could do what you're talking about with a FUNCTION.

CREATE FUNCTION getQtyOrders(customerID int) RETURNS int AS $
DECLARE
qty int;
BEGIN
SELECT COUNT(*) INTO qty
FROM Orders
WHERE accnum = customerID;
RETURN qty;
END;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文