Search code examples
mysqlmysql-error-1111

Invalid use of group function in MySQL 4 (not in 5)


Any idea why query

SELECT 
  m.* 
FROM 
  products_description pd, 
  products p 
  left join manufacturers m on p.manufacturers_id = m.manufacturers_id,
  products_to_categories p2c 
WHERE 
  p.products_carrot = '0' and 
  p.products_status = '1' and 
  p.products_id = p2c.products_id and 
  pd.products_id = p2c.products_id and 
  pd.language_id = '4' and 
  p2c.categories_id = '42'
GROUP BY manufacturers_id 
ORDER BY COUNT(*)

might give following error:

#1111 - Invalid use of group function

at MySQL 4.0.24 and not on MySQL 5.0.51 ?


Solution

  • Self response. Mentioning column that I want to order by in SELECT clause and aliasing it did the trick:

    SELECT 
      m.*, COUNT(*) as cnt
    FROM 
      products_description pd, 
      products p 
      left outer join manufacturers m on p.manufacturers_id = m.manufacturers_id,
      products_to_categories p2c 
    WHERE 
      p.products_carrot = '0' and 
      p.products_status = '1' and 
      p.products_id = p2c.products_id and 
      pd.products_id = p2c.products_id and 
      pd.language_id = '4' and 
      p2c.categories_id = '42'
    GROUP BY p.manufacturers_id 
    ORDER BY cnt