Unknown column 'd.id' in 'order clause' SQL query : SELECT a.id, a.name, a.strid FROM places a WHERE a.cityID = '11' AND a.id IN (SELECT d.place_id FROM (SELECT b.place_id FROM deal_place b, deals c WHERE b.deal_id = c.id AND c.deal_subgroupID IN (70)) AS d) AND a.id >= (SELECT (RAND() * (SELECT MAX(id) FROM places)) AS id) ORDER BY d.id ASC LIMIT 3