PostgreSQL:从表中返回选择计数(*);

发布于 2024-10-12 20:36:24 字数 1624 浏览 7 评论 0原文

请帮我解决另外 1 个 PL/pgSQL 问题。

我有一个 PHP 脚本作为每日 cronjob 运行,并从 1 个主表和几个引用其“id”列的其他表中删除旧记录:

create or replace function quincytrack_clean()
        returns integer as $BODY$
        begin
                create temp table old_ids 
                (id varchar(20)) on commit drop;

                insert into old_ids
                select id from quincytrack
                where age(QDATETIME) > interval '30 days';

                delete from hide_id where id in
                (select id from old_ids);

                delete from related_mks where id in
                (select id from old_ids);

                delete from related_cl where id in
                (select id from old_ids);

                delete from related_comment where id in
                (select id from old_ids);

                delete from quincytrack where id in
                (select id from old_ids);

                return select count(*) from old_ids;
        end;
$BODY$ language plpgsql;

以下是我从 PHP 脚本中调用它的方法:

$sth = $pg->prepare('select quincytrack_clean()');
$sth->execute();
if ($row = $sth->fetch(PDO::FETCH_ASSOC))
        printf("removed %u old rows\n", $row['count']);

为什么会出现以下错误?

SQLSTATE[42601]: Syntax error: 7 
ERROR:  syntax error at or near "select" at character 9
QUERY:  SELECT  select count(*) from old_ids
CONTEXT:  SQL statement in PL/PgSQL function
"quincytrack_clean" near line 23

谢谢你!亚历克斯

please help me with 1 more PL/pgSQL question.

I have a PHP-script run as daily cronjob and deleting old records from 1 main table and few further tables referencing its "id" column:

create or replace function quincytrack_clean()
        returns integer as $BODY$
        begin
                create temp table old_ids 
                (id varchar(20)) on commit drop;

                insert into old_ids
                select id from quincytrack
                where age(QDATETIME) > interval '30 days';

                delete from hide_id where id in
                (select id from old_ids);

                delete from related_mks where id in
                (select id from old_ids);

                delete from related_cl where id in
                (select id from old_ids);

                delete from related_comment where id in
                (select id from old_ids);

                delete from quincytrack where id in
                (select id from old_ids);

                return select count(*) from old_ids;
        end;
$BODY$ language plpgsql;

And here is how I call it from the PHP script:

$sth = $pg->prepare('select quincytrack_clean()');
$sth->execute();
if ($row = $sth->fetch(PDO::FETCH_ASSOC))
        printf("removed %u old rows\n", $row['count']);

Why do I get the following error?

SQLSTATE[42601]: Syntax error: 7 
ERROR:  syntax error at or near "select" at character 9
QUERY:  SELECT  select count(*) from old_ids
CONTEXT:  SQL statement in PL/PgSQL function
"quincytrack_clean" near line 23

Thank you! Alex

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

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

发布评论

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

评论(1

粉红×色少女 2024-10-19 20:36:24
RETURN (select count(*) from old_ids);
RETURN (select count(*) from old_ids);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文