tags:

views:

95

answers:

1

Query:

SELECT * FROM table
  WHERE fieldA LIKE '%%' 
  AND fieldB LIKE '%%' 
  AND fieldC LIKE '%%'

This returns only records that have all fields completed. My thought would be that it should return all records in the table.

Does the '%%' actually represent that a value is needed?

UPDATE1:

Thanks to some good questions the solution was found:

Query should be like:

SELECT * FROM table
  WHERE if(fieldA IS NOT NULL,fieldA LIKE '%%',fieldA IS NULL)
  ...
+8  A: 

LIKE '%%' matches any string, even ones with zero length. The result of your query is it is returning all rows where the three fields each have a string in them.

My guess is that the fields that are not completed are NULL. Maybe you should be checking for IS NOT NULL instead of LIKE '%%'?

Matthew Jones
Sorry should have clarified more - The LIKE statements are related to a user controllable filter which allows them to search by one or more fields.
John M
So your example above is the condition in which they select no filters?
Matthew Jones
Correct. Usually at least one field would be partially completed.
John M
Can you check to see if no filters exist, and if that is true, remove the WHERE clause entirely from the query?
Matthew Jones
I could but then if only one field criteria is entered the WHERE criteria will still fail. I am going to test out using an IF statement to check if the field IS NOT NULL before allowing the criteria to be applied.
John M