views:

76

answers:

4

Hello , in my database i have 10 records with almost exact same data , they differ only by one field ( the field is not in the query) and when i run the following query

SELECT * FROM friends WHERE user_id= 'MyUserName' AND follow_back = 0 AND until_date= '2009-10-13'  LIMIT 12

it shows only 9 records , any one stumbled upon similar problem ? Thanks & waiting for your answers !

A: 

Are you sure, that all values in column user_id are the same? Maybe that one missing record has user_id = 'MyUserName ' (note the space).

oneee
Yes im sure cuz they are auto added :)
Aviatrix
I believe that MySQL automatically trims spaces from strings. SELECT 'test' = 'test ' returns 1.
Ian Clelland
@Ian: And you're right: if the column is a CHAR or VARCHAR data type, trailing space doesn't matter. See also http://dev.mysql.com/doc/refman/5.0/en/char.html
Piskvor
+3  A: 

The short answer is there's nothing wrong with your query, so

user_id!='MyUserName'

or

follow_back != 0

or

until_date != '2009-10-13'

Try just querying on one criterion at a time and see if you can norrow it down. Perhaps follow_back is NULL?

Draemon
ugh , yes , you are right , one of the records was NULL i didn't saw it thanks !
Aviatrix
+1  A: 

When trying to debug problems like these, what I would usually do is to try solving it using a divide and conquer approach.

So try and remove one where condition at a time, then execute the query. That way you will be able to isolate the offending condition.

Good luck

Mark Basmayor
A: 

I had the same problem a minute ago. It turned out it wasn't the query that was the problem, but the IF where I check if anything's returned. Might want to check that.

marius