sql - 在选择查询上两次调用相同函数的替代方法
问题描述
我有一个视图的选择查询,就像
SELECT
c.id,
c.name,
p.name AS person_name,
fetch_selected_student_of_class(c.id) as student,
fetch_selected_section(fetch_selected_student_of_class(c.id), c.id) as section
FROM
class c
left JOIN persons p ON c.room_id = p.id
这里函数fetch_selected_student_of_class
重复了两次。所以该函数将被执行两次。有没有办法避免这种情况?
解决方案
您可以使用子查询:
SELECT c.id, c.name, p.name AS person_name, c.student,
fetch_selected_section(c.student, c.id) as section
FROM (SELECT c.*, fetch_selected_student_of_class(c.id) as student
FROM class c
) c LEFT JOIN
persons p
ON c.room_id = p.id
推荐阅读
- c# - 如何遍历视图模型中的列表属性
- python - 如何在不继承 QThread 并使用 QPushButtons 执行长时间运行任务的情况下构建大型 pyqt5 GUI
- pine-script - 在给定特定时间范围(例如 1 小时)的情况下,如何获得最高收盘价和最低收盘价?
- reactjs - React Native - 使用键从数组中删除项目
- powershell - 已弃用的 wmic 的 cmd / batch 替代品是什么?
- javascript - 从乱码元素中隐藏 1 个元素
- lilypond - Lilypond - 不能使用变量作为 \addlyrics 的一部分
- java - 将 org.apache.parquet.example.data.simple.SimpleGroup 转换为 org.apache.avro.generic.GenericData.Record
- javascript - 按数据激活 5 个按钮中的一个按钮
- azure-data-factory - 将 csv 中 2 列或更多列的值连接到 Azure 数据工厂中的单列