sha*_*arp 7 sql postgresql postgresql-9.3
我需要在postgres中按计算列过滤.使用MySQL很容易,但如何使用Postgres SQL实现?
伪代码:
select id, (cos(id) + cos(id)) as op from myTable WHERE op > 1;
Run Code Online (Sandbox Code Playgroud)
任何SQL技巧?
如果您不想重复表达式,可以使用派生表:
select *
from (
select id, cos(id) + cos(id) as op
from myTable
) as t
WHERE op > 1;
Run Code Online (Sandbox Code Playgroud)
这不会对性能产生任何影响,它只是SQL标准所要求的语法糖.
或者,您可以将上面的内容重写为公用表表达式:
with t as (
select id, cos(id) + cos(id) as op
from myTable
)
select *
from t
where op > 1;
Run Code Online (Sandbox Code Playgroud)
你喜欢哪一个主要是品味问题.CTE的优化方式与派生表的优化方式相同,因此第一个可能更快,特别是如果表达式上有索引cos(id) + cos(id)
select id, (cos(id) + cos(id)) as op
from selfies
WHERE (cos(id) + cos(id)) > 1
Run Code Online (Sandbox Code Playgroud)
您应该在子句中指定计算,where因为您不能使用alias.
| 归档时间: |
|
| 查看次数: |
7051 次 |
| 最近记录: |