Column 'categoryID' in where clause is ambiguous SQL query : SELECT COUNT(brand) as cnt, brand FROM SS_products a LEFT JOIN SS_category_product d on a.productID=d.productID WHERE enabled='1' AND Price>0 AND brand!='' AND a.categoryID!=1 AND (a.categoryID IN(1429,1669) or d.categoryID IN(1429,1669)) AND categoryID IN (1380,1327,1328,1345,1386,1387,1462,1464,1428,1429,1669,1458,1459,1566,1336,1346,1349,1353,1433,1344,1362,1380) GROUP BY brand