1

我想在一个查询中查询有多少商品的价格高于平均价格和低于平均价格。

我的查询:

SELECT COUNT(*)
FROM item i
WHERE refine = 0 AND item = 5170
GROUP BY id
HAVING price > AVG(price) - STD(price) AND i.price < AVG(price) + STD(price)

但我在“有条款”中收到错误未知列“价格”

如果我在 SELECT 中使用 AVG 和 STD 函数:

SELECT COUNT(*), SUM(price > AVG(price) - STD(price) AND i.price < AVG(price) + STD(price)) as middle
FROM item i
WHERE refine = 0 AND item = 5170

我再次收到错误。如何进行正确的查询?

这里链接在sqlFiddle

4

2 回答 2

2

试试这个

select count(*) from item as t1 inner join
(
select id,AVG(price) - STD(price) as sum1, AVG(price) + STD(price)  as sum2
from item 
group by id
) as t2
on t1.id=t2.id and t1.price>sum1 and t1.price<sum2
于 2012-07-23T11:16:18.860 回答
-1
SELECT COUNT(*), SUM(price > AVG(price)) - STD(price) AND i.price < AVG(price) + STD(price)) as    middle
 FROM item i WHERE refine = 0 AND item = 5170

你忘了一个“)”->SUM(price > AVG(price))

于 2012-07-23T10:50:56.170 回答