PostgreSQL视图:在另一个计算字段中引用一个计算字段 [英] PostgreSQL Views: Referencing one calculated field in another calculated field

查看:93
本文介绍了PostgreSQL视图:在另一个计算字段中引用一个计算字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我与#1895500 ,但使用PostgreSQL而不是MySQL。

I have the same question as #1895500, but with PostgreSQL not MySQL.

如何定义具有计算字段的视图,例如:

How can I define a view that has a calculated field, for example:

 (mytable.col1 * 2) AS times_two

...并创建基于第一个字段的另一个计算字段:

... and create another calculated field that's based on the first one:

 (times_two * 2) AS times_four

...?

推荐答案

根据formla的沉重程度,您可以使用子查询:

Depending on how heavy the formla is, you could use a subquery:

select inner.*, times_two * 2 from
(select mycol * 2 as times_two from table) sub

或重写计算:

select mycol * 2, mycol * 2 * 2 from table

这篇关于PostgreSQL视图:在另一个计算字段中引用一个计算字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆