简体   繁体   中英

mysql avg() not working as expected

I'm having some trouble with MySql right now. I have an query that works just fine, I'm using it for a while, but today I got stuck with this.

The query is:

select avg(valor), tipo_id, users_id, datetime from entries where users_id = '1' and tipo_id = 1 and date_format(datetime,"%Y-%m-%d") between "'2010-09-20" and "2010-10-20" and date_format(datetime,"%h:%i") between "11:59" and "18:59" and excluded= 'n'

The query return a avg value for valor field, that's ok. But, when I change the users_id value to 635 I can't get an avg() value. I have some data with this users_id, and they fit on datetime range.

I really don't know what may be wrong with my query, and with almost 700 users, this is the firts time that I see this happen.

Try adding a GROUP BY users_id

select avg(valor), tipo_id, users_id, datetime from entries
where
    users_id = '1'
    and
    tipo_id = 1
    and
    date_format(datetime,"%Y-%m-%d") between "'2010-09-20" and "2010-10-20"
    and
    date_format(datetime,"%h:%i") between "11:59" and "18:59"
    and
    excluded= 'n'
GROUP BY users_id

Run it without the avg(valor) part, see what are the results, maybe some of the valor data is the problem. Maybe you have a string or somthing else. And what exactly do you get as reply? Any error?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM