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

Ale*_*ber 2 postgresql plpgsql

请再帮我一个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;
Run Code Online (Sandbox Code Playgroud)

这是我从PHP脚本中调用它的方式:

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

为什么会出现以下错误?

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
Run Code Online (Sandbox Code Playgroud)

谢谢!亚历克斯

Qua*_*noi 5

RETURN (select count(*) from old_ids);
Run Code Online (Sandbox Code Playgroud)