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(1351,1595) or d.categoryID IN(1351,1595)) AND categoryID IN (1350,1351,1595,1352,1354,1355,1356,1357,1358,1553,1582,1592,1350) GROUP BY brand