PostgreSQL reusing computation result in select query

后端 未结 2 806
情歌与酒
情歌与酒 2020-12-19 10:52

For example using SQL I can do:

SELECT (a+b) as c FROM table WHERE c < 5 AND (c*c+t) > 100;

Is there any way to do that using Postgre

2条回答
  •  北荒
    北荒 (楼主)
    2020-12-19 11:15

    This could be an alternative you might use:

    SELECT foo.c
    FROM (
        SELECT (a+b) as c FROM table
    ) as foo
    WHERE foo.c < 5 
    AND (foo.c*foo.c+t) > 100
    

    From a performance point of view, I think it's not an optimal solution (because of the lack of WHERE clause of foo subquery, hence returning all table records). I don't know if Postgresql does some query optimization there.

提交回复
热议问题