tags:

views:

24

answers:

2

Hello,

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.

A: 

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
madsleejensen
he does not need GROUP BY in this case. He is grouping on the entire results if I get it right.
Yasen Zhelev
Yeah grouping is not what I nedd. I'm not getting any result, and that's what I need to resolve.
apocalypse
A: 

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?

Yasen Zhelev
I get no error, if I run wihout the avg I get no result either, if I change the users_id, everything is ok. Every users_id that I try returns me something, and all have some data just like the problematic users_id
apocalypse
If you are getting no data for that user, then the table data is the problem, not the query. Are you sire that you have data for user_id = 635 in your table. Check that with some DBA tool like phpMyAdmin.
Yasen Zhelev
Yeah, I have data, I'm using sequel pro to look into it and run the queries too.
apocalypse
And when you run "select tipo_id, users_id, datetime from entries where users_id = '635' 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'" you are not getting anything, right?How about the data in the rest of the columns for that user, maybe excluded is 'y' or something like that. Check everything again.
Yasen Zhelev
I tried some queries with less parameters, when I run without the between for hours, the query works. But the strange thing is that I changed the values in datetime to '2010-10-19 14:15:00' (without the quotes), so all entries has this value on datetime column.Using dateformat(datetime, "%h:$i") between "00:00" and "12:59" I get the values, but if the time part of datetime value is 14:15:00 how could this happen?
apocalypse
Use dateformat(datetime, "%H:$i") upper 'h' and try with hour bigger than 12 like 14:15:00 that is the problem. <http://dev.mysql.com/doc/refman/5.1/en/date-and-time-functions.html#function_date-format> for more information
Yasen Zhelev
Simple as that! Thanks a lot Yasen! :D
apocalypse