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 'Almunia' AND CASE WHEN properties_loc4.name_en_loc4 IS NOT NULL THEN properties' at line 70
SELECT properties_loc1.name_en_loc1 AS country, CASE WHEN properties_loc2.name_en_loc2 IS NOT NULL THEN properties_loc2.name_en_loc2 ELSE province1.name_en_loc2 END AS province, CASE WHEN properties_loc3.name_en_loc3 IS NOT NULL THEN properties_loc3.name_en_loc3 ELSE areas1.name_en_loc3 END AS area, CASE WHEN properties_loc4.name_en_loc4 IS NOT NULL THEN properties_loc4.name_en_loc4 ELSE towns.name_en_loc4 END AS town, CASE WHEN properties_types.types_en_typ IS NOT NULL THEN properties_types.types_en_typ ELSE types.types_en_typ END AS type, properties_properties.lat_long_gp_prop AS lat, properties_status.status_en_sta as sale, properties_status.id_sta as saleId, properties_status.id_sta, properties_properties.m2_prop, properties_properties.m2_parcela_prop as m2p_prop, properties_properties.precio_prop as old_precio, properties_properties.preci_reducidoo_prop as precio, properties_properties.habitaciones_prop, properties_properties.aseos_prop, properties_properties.construccion_prop, properties_properties.ascensor_prop, properties_properties.energia_prop, precio_1_prop, precio_2_prop, precio_3_prop, precio_4_prop, precio_5_prop, precio_6_prop, precio_7_prop, precio_8_prop, precio_9_prop, precio_10_prop, precio_11_prop, precio_12_prop, id_prop, image_img, alt_en_img as alt, properties_properties.descripcion_en_prop as description, title_en_prop as metatitle, description_en_prop as metadescription, properties_properties.referencia_prop as ref, properties_properties.vendido_prop, properties_properties.reducido_prop, properties_properties.nuevo_prop, properties_properties.cerca_mar_prop, properties_properties.vistas_mar_prop, properties_properties.exclusivo_prop, properties_properties.piscina_prop, properties_properties.embargo_prop, properties_properties.alquilado_prop, properties_properties.reservado_prop, properties_properties.watermark_prop FROM properties_loc4 towns LEFT OUTER JOIN properties_properties ON properties_properties.localidad_prop = towns.id_loc4 LEFT OUTER JOIN properties_loc3 areas1 ON towns.loc3_loc4 = areas1.id_loc3 LEFT OUTER JOIN properties_loc3 ON areas1.parent_loc3 = properties_loc3.id_loc3 LEFT OUTER JOIN properties_loc2 province1 ON areas1.loc2_loc3 = province1.id_loc2 LEFT OUTER JOIN properties_loc2 ON province1.parent_loc2 = properties_loc2.id_loc2 LEFT OUTER JOIN properties_loc1 ON province1.loc1_loc2 = properties_loc1.id_loc1 LEFT OUTER JOIN properties_loc4 ON towns.parent_loc4 = properties_loc4.id_loc4 LEFT OUTER JOIN properties_types types ON properties_properties.tipo_prop = types.id_typ LEFT OUTER JOIN properties_types ON types.parent_typ = properties_types.id_typ LEFT OUTER JOIN properties_images ON properties_properties.id_prop = properties_images.property_img AND order_img = 1 LEFT OUTER JOIN properties_status ON properties_properties.operacion_prop = properties_status.id_sta WHERE activado_prop = 1 AND preci_reducidoo_prop >= '280000' AND preci_reducidoo_prop <= '420000' AND CASE WHEN properties_types.types_en_typ IS NOT NULL THEN properties_types.types_en_typ ELSE types.types_en_typ END = 'Villa' AND CASE WHEN properties_loc3.name_en_loc3 IS NOT NULL THEN properties_loc3.name_en_loc3 ELSE areas1.name_en_loc3 END = 'Rafol d'Almunia' AND CASE WHEN properties_loc4.name_en_loc4 IS NOT NULL THEN properties_loc4.name_en_loc4 ELSE towns.name_en_loc4 END = 'Rafol d'Almunia' AND id_prop != '1990' GROUP BY id_prop ORDER BY order_img, RAND() LIMIT 8