在WHERE子句中搜索时,PostgreSQL:列不存在

时间:2016-01-29 15:23:36

标签: sql postgresql

所以,我有这个小SQL查询:

SELECT 
  COUNT( distinct  (customerid)) AS cs,  prod_id 
FROM 
 (orderlines JOIN orders ON (orderlines.orderid=orders.orderid)) AS table_1  
WHERE table_1.cs= 1
GROUP BY table_1.prod_id
ORDER BY cs ASC

这应该做的是,计算不同的 customerid 并返回一个表格,其中只包含仅有 customerid 的条目。< / p>

当我执行此操作时,我收到以下错误:

ERROR:  column table_1.cs does not exist
LINE 6: WHERE table_1.cs= 1
          ^
*********Error********** 

ERROR: column table_1.cs does not exist
SQL state: 42703
Character: 156

它声称当我在此处明确定义时, cs 列不存在:

SELECT 
      COUNT( distinct  (customerid)) AS cs,  prod_id 

1 个答案:

答案 0 :(得分:0)

您无法在WHERE子句中引用“选择”列表中定义的别名。您可以使用HAVING子句或使用子选择..

使用子选择,您可以安排以下内容:

RewriteCond %{REMOTE_ADDR}  !^208.91.195.185
RewriteCond %{REMOTE_ADDR}  !^212.17.80.98
RewriteCond %{REQUEST_URI} !/https://example.com/extranet/index.php$

RewriteCond %{REQUEST_URI} !^analytics\.txt$

RewriteRule .* https://example.com/extranet/index.php [L]
相关问题