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 ') Group by adv.name order by rand() limit 0,2' at line 7 SELECT adv.*, adv_categories.category_id as cat_id FROM adv LEFT JOIN adv_categories ON adv.id = adv_categories.adv_id WHERE adv.status = 2 && adv.timeactive> 1544409694 && adv_categories.category_id IN( SELECT category_id FROM `adv_categories` WHERE adv_id = ) Group by adv.name order by rand() limit 0,2