在 Join 中调用返回表的 PostgreSQL 函数

问题描述 投票:0回答:3

我有以下问题:

WITH matching_data as (
 select * from consultants_matching_data_for_project(6001)
)

select 
  id,
  matching_data.city_matching

from 
  consultant_profiles
LEFT OUTER JOIN matching_data on matching_data.consultant_profile_id = consultant_profiles.id;

有没有其他方法可以在不使用 WITH 子句的情况下加入 matching_data 结果?

sql postgresql subquery common-table-expression
3个回答
0
投票

似乎 suquery 工作正常:

SELECT
  consultant_profiles.id,
  matching_data.city_matching
FROM
  consultant_profiles
  LEFT OUTER JOIN (
    SELECT *
    FROM consultants_matching_data_for_project(6001)
  ) AS matching_data
  ON matching_data.consultant_profile_id = consultant_profiles.id;

0
投票

无论如何都不需要 CTE,也不需要子查询。您可以像使用表格一样使用该功能:

SELECT
  consultant_profiles.id,
  matching_data.city_matching
FROM
  consultant_profiles
LEFT JOIN consultants_matching_data_for_project(6001) AS matching_data
  ON matching_data.consultant_profile_id = consultant_profiles.id;

0
投票

您可以将函数调用直接移动到外部查询的

from
子句:

SELECT p.id, x.city_matching
FROM consultant_profiles p
LEFT JOIN consultants_matching_data_for_project(6001) x 
    ON x.consultant_profile_id = p.id;

来自文档

作为表源的 SQL 函数:所有 SQL 函数都可以在查询的 FROM 子句中使用

© www.soinside.com 2019 - 2024. All rights reserved.