如何在 where 子句中使用临时列

发布于 2024-07-14 18:09:52 字数 770 浏览 15 评论 0原文

为什么不能在 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_countbrand_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' 

Why can't I use a temporary column in the where clause?

For example, this query:

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

This brings up two columns, one called product_brand and one called brand_count. brand_count is created on the fly and is always 1 or 0 depending on whether or not there are 50 or products with that brand.

All this makes sense to me, except that I can't select only if brand_count = 1 as in this query below:

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

which gives me this error:

#1054 - Unknown column 'brand_count' in 'where clause' 

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

情丝乱 2024-07-21 18:09:52

使用 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

WHEREGROUP BY 之前评估。 HAVING 之后进行评估。

Use HAVING instead:

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 is evaluated before the GROUP BY. HAVING is evaluated after.

雨后咖啡店 2024-07-21 18:09:52

因为在 SQL 中,首先“选择”列,然后“投影”列。

Because in SQL the columns are first "selected" and then "projected".

梦醒时光 2024-07-21 18:09:52

您必须使用完整子句,因此您需要:

Select 
  product_brand, 
  (CASE WHEN COUNT(product_brand)>50 THEN 1 ELSE 0 END) AS brand_count 
FROM products 
WHERE 
  (CASE WHEN COUNT(product_brand)>50 THEN 1 ELSE 0 END)  = 1 
GROUP BY product_brand

这对于任何 SQL 语句中的任何计算字段都是相同的。

简单来说:

Select Max(Points) as Highest where Highest > 10

不会起作用,但是:

Select Max(Points) as Highest where Max(Points) > 10

会起作用。 你的情况也是一样。

You have to use the full clause, so you will need:

Select 
  product_brand, 
  (CASE WHEN COUNT(product_brand)>50 THEN 1 ELSE 0 END) AS brand_count 
FROM products 
WHERE 
  (CASE WHEN COUNT(product_brand)>50 THEN 1 ELSE 0 END)  = 1 
GROUP BY product_brand

This is the same for any calculated field in any SQL statement .

To simplify:

Select Max(Points) as Highest where Highest > 10

won't work, but:

Select Max(Points) as Highest where Max(Points) > 10

will. It's the same in your case.

毁梦 2024-07-21 18:09:52

因为在完成处理之前它不知道该列是什么。

如果您想通过该名称访问该列,则必须使用子查询,否则您将必须在不使用您指定的名称的情况下限定该列,从而重复您的 case 语句。

Because it has no idea what that column is until after it's done the processing.

If you want to access the column by that name you would have to use a subquery, otherwise you are going to have to qualify the column without the name you gave it, repeating your case statement.

弄潮 2024-07-21 18:09:52

如果我正确地理解了您的意图,您可以重写此查询以读取:

Select 
 product_brand,
 COUNT(product_brand) AS brand_count 
FROM 
 products 
GROUP BY 
 product_brand
HAVING 
 COUNT(product_brand) > 50

这将为您提供所有具有 count > 的 product_brands 。 50 并且还会告诉你
每个的计数。

If I read your intent correctly, you can re-write this query to read:

Select 
 product_brand,
 COUNT(product_brand) AS brand_count 
FROM 
 products 
GROUP BY 
 product_brand
HAVING 
 COUNT(product_brand) > 50

This will give you all product_brands that have a count > 50 and will also show you
the count for each.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文