Server shutdown in progress
SELECT DISTINCT
CASE WHEN properties_loc3.name_no_loc3 IS NOT NULL THEN properties_loc3.name_no_loc3 ELSE areas1.name_no_loc3 END AS area,
CASE WHEN properties_loc3.id_loc3 IS NOT NULL THEN properties_loc3.id_loc3 ELSE areas1.id_loc3 END AS id
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
WHERE properties_properties.activado_prop = 1 AND alquilado_prop = 0 AND vendido_prop = 0
GROUP BY id
ORDER BY IF(id = 66, 0,1), IF(id = 2, 0,1), IF(id = 21, 0,1), area ASC