select  o_customer,o_price from orders  having o_price >=avg(o_price)
select  o_customer,o_price from orders where o_price >=(select avg(o_price) from orders)
我感觉没有区别啊,怎么在mysql会有区别。
o_ id     o_date          o_price  o_customer
1 2008-12-29 1000 bush
2 2008-11-23 1600 carter
3 2008-10-05 700 bush
4 2008-09-28 300 bush
5 2008-08-06 2000 adams
6 2008-07-21 100 carter

解决方案 »

  1.   

    having o_price >=avg(o_price)
    这个是分组的条件吧 是用来过滤显示的组的
    where 是过滤行的,应该有区别的,但你这样写,不加group by 没试验过。不知道是什么意思!
      

  2.   

    where 子句的作用是在对查询结果进行分组前,将不符合where条件的行去掉,即在分组之前过滤数据,条件中不能包含聚组函数,使用where条件显示特定的行。having 子句的作用是筛选满足条件的组,即在分组之后过滤数据,条件中经常包含聚组函数,使用having 条件显示特定的组,也可以使用多个分组标准进行分组。having 子句被限制子已经在SELECT语句中定义的列和聚合表达式上。通常,你需要通过在HAVING子句中重复聚合函数表达式来引用聚合值,就如你在 SELECT语句中做的那样。例如:SELECT A COUNT(B) FROM TABLE GROUP BY A HAVING COUNT(B)>2
      

  3.   

    avg(o_price) 与(select avg(o_price) from orders)这两个的区别吧
    第一个应该是每条记录的平均值。
    第二个应该是所有记录的平均值吧
    我SQL不咋滴。不知道对不对。
      

  4.   

    select o_customer,o_price from orders having o_price >=avg(o_price)首先有错误可以写成(1)select o_customer,o_price from orders group by o_customer,o_price having o_price >=avg(o_price)或(2)select o_price from orders group by o_price having o_price >=avg(o_price) having必须与group by一起用,其中(1)出现的结果是:包含o_customer,o_price两个字段(2)出现的结果是:只包含该o_price字段,查出来的都是该客户的平均价格我认为没有什么意。select o_customer,o_price from orders where o_price >=(select avg(o_price) from orders)查出来的是大于所有客户的平均价格