我一直在将一些MySQL查询迁移到PostgreSQL以使用Heroku。我的大多数查询工作正常,但是当使用group by时,我仍然遇到类似的重复错误:
错误:“ XYZ”列必须出现在GROUP BY子句中或在聚合函数中使用
有人可以告诉我我在做什么错吗?
可以100%运行的MySQL:
SELECT `availables`.*
FROM `availables`
INNER JOIN `rooms` ON `rooms`.id = `availables`.room_id
WHERE (rooms.hotel_id = 5056 AND availables.bookdate BETWEEN '2009-11-22' AND '2009-11-24')
GROUP BY availables.bookdate
ORDER BY availables.updated_at
PostgreSQL错误:
ActiveRecord :: StatementInvalid:PG错误:错误:列“ availables.id”必须出现在GROUP BY子句中或在聚合函数中使用:
SELECT“ availables”。* FROM“ availables” INNER JOIN“ rooms” ON“ rooms”。 id =“ availables” .room_id在哪里(rooms.hotel_id = 5056 AND availables.bookdate在E'2009-10-21'和E'2009-10-23'之间)GROUP BY availables.bookdate订单BY availables.updated_at
Ruby代码生成SQL:
expiration = Available.find(:all,
:joins => [ :room ],
:conditions => [ "rooms.hotel_id = ? AND availables.bookdate BETWEEN ? AND ?", hostel_id, date.to_s, (date+days-1).to_s ],
:group => 'availables.bookdate',
:order => 'availables.updated_at')
预期输出(来自正常的MySQL查询):
+ ----- + ------- + ------- + ------------ + --------- + ---- ----------- + --------------- + | id | 价格| 点| 预定日期 room_id | created_at | Updated_at | + ----- + ------- + ------- + ------------ + --------- + ---- ----------- + --------------- + | 414 | 38.0 | 1 | 2009-11-22 | 1762年| 2009-11-20 ... | 2009-11-20 ... | | 415 | 38.0 | 1 | 2009-11-23 | 1762年| 2009-11-20 ... | 2009-11-20 ... | | 416 | 38.0 | 2 | 2009-11-24 | 1762年| 2009-11-20 ... | 2009-11-20 ... | + ----- + ------- + ------- + ------------ + --------- + ---- ----------- + --------------- + 一套三排