将PostgreSQL查询存储到变量中
我正在实现搜索功能,这将为过滤功能提供结果 是否可以将中间执行结果存储到变量中,并根据函数提供的参数进行选择 我目前被困在这个实现上:将PostgreSQL查询存储到变量中,postgresql,Postgresql,我正在实现搜索功能,这将为过滤功能提供结果 是否可以将中间执行结果存储到变量中,并根据函数提供的参数进行选择 我目前被困在这个实现上: CREATE FUNCTION public.search_companies4( head_country_id bigint DEFAULT NULL::bigint, name character varying DEFAULT NULL::character varying, "categoryIds" integer[] DEF
CREATE FUNCTION public.search_companies4(
head_country_id bigint DEFAULT NULL::bigint,
name character varying DEFAULT NULL::character varying,
"categoryIds" integer[] DEFAULT NULL::integer[],
"cityIds" integer[] DEFAULT NULL::integer[],
departments public.department[] DEFAULT NULL::public.department[],
"stockTemperatures" public.stock_temperature[] DEFAULT NULL::public.stock_temperature[],
verified boolean DEFAULT NULL::boolean,
with_comments boolean DEFAULT NULL::boolean
)
RETURNS SETOF public.company
LANGUAGE plpgsql STABLE
AS $$
declare
"companies" public.company;
begin
if ("search_companies4"."cityIds" is not null) then
select into "companies" from "companies" AS c
left join "companyCities" cC ON c.id = cC."companyId"
where "search_companies4"."cityIds" @> array[cC."cityId"];
end if;
if ("search_companies4"."categoryIds" is not null) then
select into "companies" from "companies" AS c
left join "companyCategories" cCat ON c.id = cCat."companyId"
where (
"search_companies4"."categoryIds" @> array[cCat."categoryId"]
) AND (
"search_companies4"."departments" is null OR
"search_companies4"."departments" @> array[cCat.department]
);
end if;
if ("search_companies4"."name" is not null) then
select into "companies" from "companies" AS c
where c.name ILIKE "search_companies4"."name";
end if;
if ("search_companies4"."head_country_id" is not null) then
select into "companies" from "companies" AS c
where c.head_country_id = "search_companies4"."head_country_id";
end if;
if ("search_companies4"."stockTemperatures" is not null) then
select into "companies" from "companies" AS c
where c."stockTemperatures" && "search_companies4"."stockTemperatures";
end if;
if ("search_companies4"."verified" is not null) then
select into "companies" from "companies" AS c
left join company_import ci ON c.id = ci.company_id
where (
CASE WHEN "search_companies4"."verified" = true THEN
-- Созданные вручную и проверенные импортированные
ci.company_id is null OR ci.verified_at is not null
ELSE true END -- И созданные вручную и все импортированные
);
end if;
if ("search_companies4"."with_comments" is not null) then
select into "companies" from "companies" AS c
where (
CASE WHEN "search_companies4"."with_comments" = true THEN
-- Только компании с комментариями
c."lastCommentAt" IS NOT NULL
ELSE true END
);
end if;
return query select from "companies" order by id;
end;
$$;
它不起作用,因为PostgreSQL抱怨公司
变量:
ERROR: relation "companies" does not exist at character 30
QUERY: select from "companies" AS c
left join "companyCategories" cCat ON c.id = cCat."companyId"
where (
"search_companies4"."categoryIds" @> array[cCat."categoryId"]
) AND (
"search_companies4"."departments" is null OR
"search_companies4"."departments" @> array[cCat.department]
)
如何使用此变量存储可与select
语句一起逐步使用的查询
或者,是否有更好的实现…您需要一个临时表,您不能在与您的问题无关的变量中这样做,但是:您应该真正避免那些可怕的带引号的标识符。他们的麻烦远比他们值得的多。