Mysql AVG to ignore zero

社会主义新天地 提交于 2019-12-02 19:15:24

Assuming that you might want to not totally exclude such rows (perhaps they have values in other columns you want to aggregate)

SELECT AVG(NULLIF(field ,0)) 
from table

You could probably control that via the WHERE clause:

select avg( field ) from table where field > 0
select avg(your_column) 
from your_table 
where your_column != 0
Barvajz

You can convert zeros to NULL, then AVG() function will work only with not NULL values.

UPDATE table SET column = NULL WHERE column='0';
SELECT AVG(column) FROM table;
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!