SELECT AVG(price) as price from data_1 where请问要去掉计算所有空值(0)的平均数该怎么写?

解决方案 »

  1.   

    SELECT AVG(price) as price from data_1 where IFNULL(price,0)>0 
      

  2.   

    0 不是空值 
    SELECT AVG(price) as price from data_1 where price<>0
      

  3.   

    where price is not null;
      

  4.   

    不明白楼主的意思你是不是想这样,比如说你的表t_test里只有一个字段,就叫price,里面有5行数据,比如他们的值为1,2,3,null,5.你是不是想计算平均值的时候,得到的数据是这样的(5+3+2+1)/5 ? 也就是说,有5行数据,但是只有4个有值,但你想计算出他们的值对5个的平均值?
    如果这样 select avg(price) from t_test 得到的数是2.7500
    这是(5+3+2+1)/4的结果
    如果你想要让他平均给5行数据的话,这样
    select avg(a.price) from (select coalesce(price,0) as price from test) a,得到的数是2.2000
    OK!搞定
      

  5.   

    SELECT AVG(price) as price from data_1 where price<>0 and price is not null
    双重啊?
      

  6.   

    SELECT AVG(price) as price from data_1
    where price=0 
          or price is null;-- 这么简单的问题也来问,你还能做什么?
      

  7.   

    SELECT AVG(price) as price from data_1
    where price<>0 
          or price is not null;
      

  8.   

    10楼的代码还是有问题,or应该改为and