簡體   English   中英

MySQL到Postgres group_by錯誤

[英]MySQL to Postgres group_by error

以下查詢在MySQL中正常運行( 由rails生成 ):

SELECT
   sum(up_votes) total_up_votes 
FROM
   "answers" 
WHERE
   "answers"."user_id" = 100
ORDER BY
   "answers"."id" ASC

然而,它在Postgres中給出了以下錯誤:

PG::GroupingError: ERROR:  column "answers.id" must appear in the GROUP BY clause or be used in an aggregate function

編輯:更新的查詢。 這是執行此查詢的Rails模型:

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

該查詢可能在MySQL中執行,但我懷疑它“運行正常”。 它僅返回一行,因此排序沒有意義。

您的問題對您實際想要做什么含糊不清,但這在兩個數據庫中都應產生與查詢相同的結果:

SELECT sum(up_votes) as total_up_votes
FROM answers
WHERE user_id = 100;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM