【问题标题】:PostgreSQL Views: Referencing one calculated field in another calculated fieldPostgreSQL 视图:在另一个计算字段中引用一个计算字段
【发布时间】:2011-01-24 01:19:02
【问题描述】:
我和#1895500 有同样的问题,但使用的是 PostgreSQL 而不是 MySQL。
如何定义具有计算字段的视图,例如:
(mytable.col1 * 2) AS times_two
...并基于第一个计算字段创建另一个计算字段:
(times_two * 2) AS times_four
...?
【问题讨论】:
标签:
sql
postgresql
calculated-columns
sql-view
【解决方案1】:
根据表单的重量,您可以使用子查询:
select inner.*, times_two * 2 from
(select mycol * 2 as times_two from table) sub
或者重写计算:
select mycol * 2, mycol * 2 * 2 from table
【解决方案2】:
使用这个语句
CREATE VIEW view_name as SELECT column_name*2 as new_col1 , column_name*4 as new_col2 from table_name ;
从 view_name 中选择 * ;
如果要使用此视图列值。使用以下东西
创建视图 new_viwe as select new_col1*2 as final_column from view_name ;
从 new_view 中选择 * ;