Postgres - 至少获得 2 个计数

Postgres - get LEAST of 2 COUNTs

我想找到 2 个 table 尺寸中最小的尺寸,如下所示:

create function least_func() returns void as $$
declare
    result INT;
begin

    select least(
               select count(*) from my_table,
               select count(*) from my_other_table) into result;

end $$ language plpgsql;

这不编译:

syntax error at or near "select" LINE 7: select count(*) from my_table,

这个函数有什么问题?

您需要在 select 语句两边加上括号。

select least(
   (select count(*) from my_table),
   (select count(*) from my_other_table)) into result;