Error: SELECT b.id, b.name, i.alt_name, b.title, l.address, l.postcode, c.name city_name, c.code , c.url, count(r.id) totrev, avg(rating) rate, p.id photo_id, l.lat, l.lng, t.photo_id g_photo_id, i.alt_name, z.radius, (3959 * acos ( cos ( radians(42.7582962) ) * cos( radians( l.`lat` ) ) * cos( radians( l.`lng` ) - radians(11.8306296) ) + sin ( radians(42.7582962) ) * sin( radians( l.`lat` ) ))) AS distance FROM company b JOIN company_premium z on z.company_id = b.id JOIN subscriptions s on s.id = z.subscription_id LEFT JOIN company_city_cat a USING (company_id) LEFT JOIN company_location l USING (company_id) LEFT JOIN company_photo t USING (company_id) LEFT JOIN company_info_2 i USING (company_id) LEFT JOIN city c on a.city_id = c.id LEFT JOIN review r on r.company_id = b.id AND r.approved > 0 LEFT JOIN photo p on p.company_id = b.id AND logo = 1 WHERE s.online = 1 AND a.`cat_id` IN ( , 1589 ) GROUP by a.company_id HAVING distance <= z.radius ORDER BY RAND() LIMIT 5 You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near ' 1589 ) GROUP by a.company_id HAVING distance <= z.radius ' at line 18