postgresql - 将 PostgreSQL 查询存储到变量中
问题描述
我正在实现搜索功能,它将为过滤功能提供结果。
是否可以将中间执行结果存储到变量中并根据提供的函数参数从中选择?
我目前坚持这个实现:
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 抱怨companies
变量:
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
语句一起使用?
或者,有没有更好的实现方式……</p>
解决方案
推荐阅读
- javascript - 如何将 React Context API 与 useReducer 一起使用以遵循与 Redux 类似的风格
- python - 为什么有些 python 在命令提示符下工作时不能在 python 阅读器或 IDLE 上工作?
- c++ - void 循环可以独立运行吗?
- python - For 循环追加到列表(奇怪的可能可以解释的行为)
- sql - BigQuery 从多个数据集中选择、加入并避免名称冲突
- python - 如何立即运行 python 脚本而不延迟?
- html - -webkit-locale 在 css 中是什么意思?
- visual-c++ - 使用运算符 [] 填充 std::vector 映射?
- laravel - 拥有 API 和网站时如何设置路由
- java - 从外部 API 获取值并通过另一个控制器显示