You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ') AND (CASE WHEN listings.sync = 1 AND listings.mls_id IS NOT NULL AND listings.' at line 16