我有一个简单的查询:
SELECT ((interest_step / 12) * 365 + (MOD(interest_step, 12) * 30)) AS "days"
FROM "negotiations"."addon" WHERE (days <= 72)
但是当我执行它时,我得到一个错误:列'days'不存在。
我该如何纠正?
我有一个简单的查询:
SELECT ((interest_step / 12) * 365 + (MOD(interest_step, 12) * 30)) AS "days"
FROM "negotiations"."addon" WHERE (days <= 72)
但是当我执行它时,我得到一个错误:列'days'不存在。
我该如何纠正?
试试这个:
SELECT *
FROM
(
SELECT
((interest_step / 12) * 365 + (MOD(interest_step, 12) * 30)) AS "days"
FROM "negotiations"."addon"
) t
WHERE (days <= 72)
那是因为WHERE
子句在子句之前执行SELECT
,因此它不会看到别名days
。您应该将其包含在子查询中。
您不能在生成它的同一选择语句中引用列别名,这为您提供了两种选择:
SELECT ((interest_step / 12) * 365 + (MOD(interest_step, 12) * 30)) AS "days"
FROM "negotiations"."addon"
WHERE (((interest_step / 12) * 365 + (MOD(interest_step, 12) * 30)) <= 72)
或者
SELECT *
FROM
(
SELECT ((interest_step / 12) * 365 + (MOD(interest_step, 12) * 30)) AS "days"
FROM "negotiations"."addon"
) A
WHERE (days <= 72)