COUNT(id) vs. COUNT(*) in MySQL

I know this question is about MySQL, but for what it’s worth, count(*) is recommended for Oracle, which goes to show that the answer to this can be database dependent (see comment above from BalusC). Since a lot of databases (MS-SQL, MySQL) have information schema tables that hold various types of metadata, there are bound … Read more

How to count identical string elements in a Ruby array

Ruby v2.7+ (latest) As of ruby v2.7.0 (released December 2019), the core language now includes Enumerable#tally – a new method, designed specifically for this problem: names = [“Jason”, “Jason”, “Teresa”, “Judah”, “Michelle”, “Judah”, “Judah”, “Allison”] names.tally #=> {“Jason”=>2, “Teresa”=>1, “Judah”=>3, “Michelle”=>1, “Allison”=>1} Ruby v2.4+ (currently supported, but older) The following code was not possible in … Read more

PGError: ERROR: aggregates not allowed in WHERE clause on a AR query of an object and its has_many objects

The error message tells you: aggregates not allowed in WHERE clause count() is an aggregate function. Use the HAVING clause for that. The query could look like this: SELECT r.* FROM recommendations r JOIN approvals a ON a.recommendation_id = r.id WHERE r.user_id = $current_user_id GROUP BY r.id HAVING count(a.recommendation_id) = 1 With PostgreSQL 9.1 or … Read more