MySQL 别名和聚合函数的查询问题
我有一个麻烦的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
WHERE
指定连接条件,但最好在LEFT[INNER] JOIN
子句中执行。WHERE
,如果需要按聚合过滤,请将条件移动到HAVING
当使用聚合和同一查询中的非聚合,不要忘记
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();
WHERE
to specify join condition, it's better to do inLEFT[INNER] JOIN
clause.WHERE
, if you need to filter by aggregate, move condition intoHAVING
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();
ORDER
子句应位于HAVING
子句之后。 (此外,您在ORDER BY rand()
之后放置一个分号;
,然后继续使用HAVING
,这实际上是另一个查询的开始,因为首先以;
结束)。ORDER
clause should go afterHAVING
clause. (besides you put a semicolon;
afterORDER BY rand()
then continued withHAVING
, which is actually a start of another query, since the first got ended with the;
).