The following query works fine in MySQL (which is generated by rails ):
SELECT
sum(up_votes) total_up_votes
FROM
"answers"
WHERE
"answers"."user_id" = 100
ORDER BY
"answers"."id" ASC
Yet it gives the following error in Postgres:
PG::GroupingError: ERROR: column "answers.id" must appear in the GROUP BY clause or be used in an aggregate function
Edit: updated query. And here is the Rails model where this query is performed:
class User < MyModel
def top?
data = self.answers.select("sum(up_votes) total_up_votes").first
return (data.total_up_votes.present? && data.total_up_votes >= 10)
end
end
The query may execute in MySQL, but I doubt that it "works fine". It returns only one row, so the ordering is meaningless.
Your question is vague on what you actually want to do, but this should produce the same results as your query, in both databases:
SELECT sum(up_votes) as total_up_votes
FROM answers
WHERE user_id = 100;
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.