为什么我不能在where子句中使用临时列?
例如,这个查询:
Select product_brand, (CASE WHEN COUNT(product_brand)>50 THEN 1 ELSE 0 END) AS brand_count FROM products WHERE 1 GROUP BY product_brand
这会产生两列,一列叫product_brand
,一列叫brand_count
.brand_count
是动态创建的,始终为1或0,具体取决于是否有50个或具有该品牌的产品.
所有这些对我来说都是有意义的,除了我不能只选择如brand_count = 1
下面的查询:
Select product_brand, (CASE WHEN COUNT(product_brand)>50 THEN 1 ELSE 0 END) AS brand_count FROM products WHERE brand_count = 1 GROUP BY product_brand
这给了我这个错误:
#1054 - Unknown column 'brand_count' in 'where clause'
derobert.. 14
HAVING
改为使用:
Select product_brand, (CASE WHEN COUNT(product_brand)>50 THEN 1 ELSE 0 END) AS brand_count FROM products GROUP BY product_brand HAVING brand_count = 1
WHERE
被评估前的GROUP BY
.HAVING
经过评估.
HAVING
改为使用:
Select product_brand, (CASE WHEN COUNT(product_brand)>50 THEN 1 ELSE 0 END) AS brand_count FROM products GROUP BY product_brand HAVING brand_count = 1
WHERE
被评估前的GROUP BY
.HAVING
经过评估.