如果不想重复该表达式,则可以使用派生表:
select *from ( select id, cos(id) + cos(id) as op from myTable ) as t WHERe op > 1;
这不会对性能产生任何影响,仅是SQL标准所需的语法糖。
或者,您可以将以上内容重写为公用表表达式:
with t as ( select id, cos(id) + cos(id) as op from myTable )select *from t where op > 1;
您更喜欢哪一个在很大程度上取决于品味。CTE的优化方式与派生表相同,因此第一个可能更快,尤其是在表达式上有索引的情况下
cos(id) + cos(id)



