本文介绍了WHERE子句中的引用别名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在psql中,尝试在 WHERE 子句中引用别名:

In psql, trying to reference alias in WHERE clause:

SELECT
    SUBSTRING(pk, 6, 2)::INT AS _year
FROM
    listing
WHERE
    _year > 90

它会导致错误:

ERROR:  column "_year" does not exist
LINE 1: ...STRING (pk, 6, 2)::INT AS _year FROM listing WHERE _year > 90...
                                                              ^
********** Error **********

ERROR: column "_year" does not exist

是否可以在 WHERE 子句?

推荐答案

这在sql中是不可能的,执行顺序是先执行,先执行where子句,然后执行select 。在执行where子句时,它不知道您定义的别名是什么,因此您将得到该错误。

This is not possible as in sql, the order of execution is first, the where clause and then the select. At the time where clause is getting executed, it does not know what you have defined as an alias and so you will get that error.

您需要像这样重写查询。

You need to rewrite your query like this..

SELECT
    SUBSTRING(pk, 6, 2)::INT AS _year
FROM
    listing
WHERE
SUBSTRING(pk, 6, 2)::INT > 90

这篇关于WHERE子句中的引用别名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 04:40
查看更多