MySQL 别名和聚合函数的查询问题

发布于 2024-09-16 23:07:11 字数 1756 浏览 6 评论 0原文

我有一个麻烦的 MySQL 查询,如下所示:

SELECT camera_id, ((avg(low_price) + avg(high_price)) / 2) as avg_price
FROM camera_general, camera_products
WHERE camera_id = ir_camera_id
AND dp_post_dt IS NOT NULL
AND dp_post_dt NOT LIKE '0000%'
AND currently_manufactured = 'Yes'
AND ((no_of_sellers >= 0) OR ((TO_DAYS(CURRENT_DATE) - TO_DAYS(dp_post_dt)) < 120))
AND avg_price < 150 
AND camera_id != 1411
AND camera_id != 9
ORDER BY rand();

这个查询产生了“'where 子句'中的未知列'avg_price'”错误。我理解这是因为 WHERE 子句中不允许使用列别名。 (如果我对此有任何错误,请纠正我。)

因此,我像这样调整了查询​​:

SELECT camera_id, ((avg(low_price) + avg(high_price)) / 2) as avg_price
FROM camera_general, camera_products
WHERE camera_id = ir_camera_id
AND dp_post_dt IS NOT NULL
AND dp_post_dt NOT LIKE '0000%'
AND currently_manufactured = 'Yes'
AND ((no_of_sellers >= 0) OR ((TO_DAYS(CURRENT_DATE) - TO_DAYS(dp_post_dt)) < 120))
AND ((avg(low_price) + avg(high_price)) / 2) < 150 
AND camera_id != 1411
AND camera_id != 9
ORDER BY rand();

用实际计算替换别名,并且此查询产生错误:“无效使用组函数”。我理解这是因为 avg() 在 WHERE 子句完成处理之后才会发生。

然后我尝试:

SELECT camera_id, ((avg(low_price) + avg(high_price)) / 2) as avg_price
FROM camera_general, camera_products
ORDER BY rand();
HAVING camera_id = ir_camera_id
AND dp_post_dt IS NOT NULL
AND dp_post_dt NOT LIKE '0000%'
AND currently_manufactured = 'Yes'
AND ((no_of_sellers >= 0) OR ((TO_DAYS(CURRENT_DATE) - TO_DAYS(dp_post_dt)) < 120))
AND avg_price < 150 
AND camera_id != 1411
AND camera_id != 9;

用 HAVING 替换 WHERE 并产生此错误“您的 SQL 语法中有错误;检查与您的 MySQL 服务器版本对应的手册,以获取在 'HAVINGcamera_id = ir_camera_id' 附近使用的正确语法” 。

在这一点上,我觉得我是在黑暗中尝试让这个查询起作用。有人会引导我朝正确的方向使其成为一个有效的查询吗?

谢谢!

I have a troublesome MySQL query as follows:

SELECT camera_id, ((avg(low_price) + avg(high_price)) / 2) as avg_price
FROM camera_general, camera_products
WHERE camera_id = ir_camera_id
AND dp_post_dt IS NOT NULL
AND dp_post_dt NOT LIKE '0000%'
AND currently_manufactured = 'Yes'
AND ((no_of_sellers >= 0) OR ((TO_DAYS(CURRENT_DATE) - TO_DAYS(dp_post_dt)) < 120))
AND avg_price < 150 
AND camera_id != 1411
AND camera_id != 9
ORDER BY rand();

This one produced the "Unknown column 'avg_price' in 'where clause'" error. I understand this is because column aliases are not allowed in the WHERE clause. (Correct me if I'm wrong with any of this as I go, please.)

So, I tweaked the query like so:

SELECT camera_id, ((avg(low_price) + avg(high_price)) / 2) as avg_price
FROM camera_general, camera_products
WHERE camera_id = ir_camera_id
AND dp_post_dt IS NOT NULL
AND dp_post_dt NOT LIKE '0000%'
AND currently_manufactured = 'Yes'
AND ((no_of_sellers >= 0) OR ((TO_DAYS(CURRENT_DATE) - TO_DAYS(dp_post_dt)) < 120))
AND ((avg(low_price) + avg(high_price)) / 2) < 150 
AND camera_id != 1411
AND camera_id != 9
ORDER BY rand();

Replacing the alias with the actual calculation and this query produced the error: "Invalid use of group function". I understand this is because the avg() can't happen until after the WHERE clause has done its processing.

So then I tried:

SELECT camera_id, ((avg(low_price) + avg(high_price)) / 2) as avg_price
FROM camera_general, camera_products
ORDER BY rand();
HAVING camera_id = ir_camera_id
AND dp_post_dt IS NOT NULL
AND dp_post_dt NOT LIKE '0000%'
AND currently_manufactured = 'Yes'
AND ((no_of_sellers >= 0) OR ((TO_DAYS(CURRENT_DATE) - TO_DAYS(dp_post_dt)) < 120))
AND avg_price < 150 
AND camera_id != 1411
AND camera_id != 9;

Replacing the WHERE with the HAVING and it produced this error "You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'HAVING camera_id = ir_camera_id'".

And at this point, I feel like I am shooting in the dark trying to make this query work. Would someone guide me in the right direction to make this a functioning query?

Thanks!

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

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

发布评论

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

评论(2

当爱已成负担 2024-09-23 23:07:11
  1. 尽管您可以使用 WHERE 指定连接条件,但最好在 LEFT[INNER] JOIN 子句中执行。
  2. 如果要按非聚合字段过滤,请将过滤器放入WHERE,如果需要按聚合过滤,请将条件移动到HAVING
  3. 当使用聚合和同一查询中的非聚合,不要忘记GROUP BY

    选择camera_id, ((avg(low_price) + avg(high_price)) / 2) as avg_price
    来自camera_general
    内部连接camera_products ON(camera_id = ir_camera_id)
    哪里 dp_post_dt 不为空
    并且 dp_post_dt 不像“0000%”
    AND current_manufactured = '是'
    AND ((no_of_sellers >= 0) OR ((TO_DAYS(CURRENT_DATE) - TO_DAYS(dp_post_dt)) < 120))
    并且camera_id!= 1411
    并且camera_id!= 9
    按camera_id分组
    平均价格 < 150
    ORDER BY rand();

  1. Even though you can use WHERE to specify join condition, it's better to do in LEFT[INNER] JOIN clause.
  2. If you want to filter by non-aggregate field, put the filter into WHERE, if you need to filter by aggregate, move condition into HAVING
  3. While using aggregate and non-aggregates in the same query, don't forget GROUP BY.

    SELECT camera_id, ((avg(low_price) + avg(high_price)) / 2) as avg_price
    FROM camera_general
    INNER JOIN camera_products ON (camera_id = ir_camera_id)
    WHERE dp_post_dt IS NOT NULL
    AND dp_post_dt NOT LIKE '0000%'
    AND currently_manufactured = 'Yes'
    AND ((no_of_sellers >= 0) OR ((TO_DAYS(CURRENT_DATE) - TO_DAYS(dp_post_dt)) < 120))
    AND camera_id != 1411
    AND camera_id != 9
    GROUP BY camera_id
    HAVING avg_price < 150
    ORDER BY rand();

原来分手还会想你 2024-09-23 23:07:11

ORDER 子句应位于 HAVING 子句之后。 (此外,您在 ORDER BY rand() 之后放置一个分号 ;,然后继续使用 HAVING,这实际上是另一个查询的开始,因为首先以 ; 结束)。

ORDER clause should go after HAVING clause. (besides you put a semicolon ; after ORDER BY rand() then continued with HAVING, which is actually a start of another query, since the first got ended with the ;).

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