Fix problem with database query for conditions