我有一个函数可以生成下面定义的评估信息类型。
create type course_assessment_info as (
total_count int,
mark_average double precision,
weight_sum double precision,
weighted_mark_average double precision
);
当我将函数用于单个查询时,如下所示,我得到了预期的结果。
select *
from course_assessment_info_by_student_id_and_course_id('9be15896-40ca-46c6-8fdd-0ffe3bd79
586', '65dbdce1-fd76-4951-9db1-d089b3794d80');
结果:
total_count | mark_average | weight_sum | weighted_mark_average
-------------+-------------------+-------------------+-----------------------
1 | 0.834768535328714 | 0.540032932289522 | 0.450802499916595
当我在使用联接的较大函数上使用它时,我没有得到单独的列名。 我的理解是,我需要使用select * from course_asses ... 但是,我很难弄清楚如何格式化以使PostgreSQL不会引发语法错误。
select course_assessment_info_by_student_id_and_course_id(s.id, c.id)
from student s
join student_course sc on s.id = sc.student_id
join course c on c.id = sc.course_id;
感谢您的帮助!
答案 0 :(得分:1)
您似乎想要LATERAL JOIN
:
SELECT t.*
FROM student s
INNER JOIN student_course sc ON s.id = sc.student_id
INNER JOIN course c ON c.id = sc.course_id
INNER JOIN LATERAL course_assessment_info_by_student_id_and_course_id(s.id, c.id) t ON true
这也应该做:
select (course_assessment_info_by_student_id_and_course_id(s.id, c.id)).*
from student s
join student_course sc on s.id = sc.student_id
join course c on c.id = sc.course_id;