我一直在将我的一些 MySQL 查询迁移到 PostgreSQL 来使用 Heroku。我的大多数查询都能正常工作,但是当我使用 group by 时,总会出现类似的反复出现的错误:
ERROR: 列“ XYZ”必须出现在 GROUPBY 子句中,或者在 聚合函数
有人能告诉我哪里做错了吗?
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 错误:
无效: PGError: ERROR: column 必须出现在 GROUPBY 子句中,或者在 总函数:
选择“可用” 在“ room”上加入“ room”. id = “ ailables”. room _ id WHERE (room s.hotel _ id = 5056 AND ailables.bookdate BETWEEN E’2009-10-21’) 和 E’2009-10-23’)组可用 Update _ at
生成 SQL 的 Ruby 代码:
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 | price | spots | bookdate | 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... | +-----+-------+-------+------------+---------+---------------+---------------+ 3 rows in set