-1

I have tried to use given query to get data, however got problem

SELECT COUNT(id) as counter , company_id
from contacts
WHERE  counter > 50
GROUP BY company_id
LIMIT 20

Error that I get

 Unknown column 'counter' in 'where clause

1 Answers1

0

Rather than asserting the count in a WHERE clause, you should instead be using a HAVING clause:

SELECT COUNT(id) AS counter, company_id
FROM contacts
GROUP BY company_id
HAVING counter > 50
-- ORDER BY <something>
LIMIT 20

Note that using LIMIT without ORDER BY is not well defined. Your query should have an ORDER BY clause.

Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360