I'm trying to discover the foreign keys which appear more than once for all rows in table and return those foreign keys via a subquery.
Using GROUP BY
on the column I was able to visualize the COUNT
of each foreign key:
SELECT orders_id, COUNT(*) as count
FROM order_history
GROUP BY orders_id
but then when I augment it with a WHERE clause in an attempt to filter, like this:
SELECT orders_id, COUNT(*) as count
FROM order_history
GROUP BY orders_id
WHERE count > 1
I get an error
#1064 - 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 'WHERE count > 1 LIMIT 0, 30' at line 4
apparently count
is ineligible for comparisons at the point WHERE
is evaluated?
: Post answer update :
Thanks for the speedy responses! HAVING
is exactly what I was missing. I found this question helpful expansion of the topic: WHERE vs HAVING