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 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_categories.adv_id WHERE adv.status = 2 && adv.timeactive> 1542452436 && adv_categories.category_id IN( SELECT category_id FROM `adv_categories` WHERE adv_id = ) Group by order by rand() limit 0,2