PostgreSQL 视图:在另一个计算字段中引用一个计算字段
-
24-09-2019 - |
题
我有同样的问题 #1895500, ,但使用的是 PostgreSQL,而不是 MySQL。
如何定义具有计算字段的视图,例如:
(mytable.col1 * 2) AS times_two
...并创建另一个基于第一个计算字段的计算字段:
(times_two * 2) AS times_four
...?
解决方案
根据公式的重量,您可以使用子查询:
select inner.*, times_two * 2 from
(select mycol * 2 as times_two from table) sub
或者重写计算:
select mycol * 2, mycol * 2 * 2 from table
其他提示
使用这个语句
CREATE VIEW view_name as SELECT column_name*2 as new_col1 , column_name*4 as new_col2 from table_name ; select * from view_name ; If you want use this view column values. use following things create view new_viwe as select new_col1*2 as final_column from view_name ; select * from new_view ;
不隶属于 StackOverflow