PostgreSQL 视图:在另一个计算字段中引用一个计算字段

发布于 2024-08-24 03:46:07 字数 395 浏览 11 评论 0原文

我有同样的问题 #1895500,但使用的是 PostgreSQL,而不是 MySQL。

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

 (mytable.col1 * 2) AS times_two

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

 (times_two * 2) AS times_four

...?

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

...?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

水晶透心 2024-08-31 03:46:07

根据公式的重量,您可以使用子查询:

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

或重写计算:

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

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

Or rewrite the computation:

select mycol * 2, mycol * 2 * 2 from table
白芷 2024-08-31 03:46:07

使用此语句

 
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 ; 


Use this statement

 
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 ; 


~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文