sql - SQLite 与 Postgres 中的 Rails Active Record 组查询

标签 sql ruby-on-rails postgresql sqlite activerecord

在使用 Active Record 的 SQLite 和 Postgres 中运行查询时出现错误。 Postgres 中有聚合函数错误,但 SQLite 中没有。我的问题是,SQLite 在这里做什么?是不是忽略了群体?请注意,GROUP BY“歌曲”不会返回错误。

SQLite 输出:

Playlist.where(user_id:user.id).group(:song)

Playlist Load (0.3ms)  SELECT "playlists".* FROM "playlists" WHERE 
"playlists"."user_id" = ? GROUP BY "song"  [["user_id", 1]]

=> #<ActiveRecord::Relation [#<Playlist id: 2, user_id: 1, song_id: 1, 
created_at: "2017-04-27 01:18:01", updated_at: "2017-04-27 01:18:01">]>

Playlist.where(user_id:user.id).group(:song_id)

 Playlist Load (0.4ms)  SELECT "playlists"."id", "playlists"."user_id",
 "playlists"."song_id" FROM "playlists" WHERE "playlists"."user_id" = ?
 GROUP BY "song"  [["user_id", 1]]

  => #<ActiveRecord::Relation [#<Playlist id: 2, user_id: 1, song_id: 
  1, created_at: "2017-04-27 01:18:01">]>

Playlist.where(user_id:user.id).group(:id,:song_id)

 Playlist Load (0.2ms)  SELECT "playlists".* FROM "playlists" WHERE 
 "playlists"."user_id" = ? GROUP BY "playlists"."id", 
 "playlists"."song_id"  [["user_id", 1]]

 => #<ActiveRecord::Relation [#<Playlist id: 1, user_id: 1, song_id: 1, 
 created_at: "2017-04-27 01:18:00", updated_at: "2017-04-27 01:18:00">, 
 #<Playlist id: 2, user_id: 1, song_id: 1, created_at: "2017-04-27 
 01:18:01", updated_at: "2017-04-27 01:18:01">]> 

Playlist.where(user_id:user.id).group(:song).count

 (0.2ms)  SELECT COUNT(*) AS count_all, "playlists"."song_id" AS      
 playlists_song_id FROM "playlists" WHERE "playlists"."user_id" = ? 
 GROUP BY "playlists"."song_id"  [["user_id", 1]]

 Song Load (2.1ms)  SELECT  "songs".* FROM "songs" 
 WHERE "songs"."id" = ? LIMIT 1  [["id", 1]] 

 => {#<Song id: 1, title: "A song", artist: "Artist", user_id: 1, 
 created_at: "2017-04-27 01:17:39">=>2}

Postgres:

Playlist.where(user_id:user.id).group(:song_id)

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

SELECT "playlists".* FROM "playlists" WHERE "playlists"."user_id" = $1 
GROUP BY "playlists"."song_id"

Playlist.where(user_id:user.id).group(:id,:song_id)

 Playlist Load (0.6ms)  SELECT "playlists".* FROM "playlists" WHERE 
 "playlists"."user_id" = $1 GROUP BY "playlists"."id",      
 "playlists"."song_id"  [["user_id", 1]]

 => #<ActiveRecord::Relation [#<Playlist id: 1, user_id: 1, song_id: 1, 
 created_at: "2017-04-27 01:25:34", updated_at: "2017-04-27 01:25:34">, 
 #<Playlist id: 2, user_id: 1, song_id: 1, created_at: "2017-04-27 
 01:25:36", updated_at: "2017-04-27 01:25:36">]>

Playlist.where(user_id:user.id).group(:song).count

 (0.5ms)  SELECT COUNT(*) AS count_all, "playlists"."song_id" AS 
 playlists_song_id FROM "playlists" WHERE "playlists"."user_id" = $1 
 GROUP BY "playlists"."song_id"  [["user_id", 1]]

 Song Load (0.3ms)  SELECT  "songs".* FROM "songs" WHERE "songs"."id" = 
 $1 LIMIT 1  [["id", 1]]

 => {#<Song id: 1, title: "A song", artist: "Artist", user_id: 1, 
 created_at: "2017-04-27 01:25:26", updated_at: "2017-04-27           
 01:25:26">=>2}

最佳答案

group by 的通用规则:您只能选择 group by 中列出的列和聚合函数。这种方法受 sql 标准支持,适用于任何 sql 数据库。

如果您选择的列未在 group by 子句中列出,例如 select * … group by song_id,它可能在某些数据库中有效,但在某些数据库中无效其他。

如果您在代码中指定选定的列,它将起作用:

Playlist.
  where(user_id:user.id).
  select("song_id").
  group(:song_id)

您可以在 PostgreSQL 文档中阅读有关 group by 的详细信息:https://www.postgresql.org/docs/current/static/sql-select.html#SQL-GROUPBY

关于sql - SQLite 与 Postgres 中的 Rails Active Record 组查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43647546/

相关文章:

Postgresql:查询 jsonb 列 - 索引并不能使其更快

PHP/SQL 服务器 : Change <div> color according to time

sql - 在存储过程中使用 WITH,postgres

ruby-on-rails - 涡轮链接 5 : Show progress Bar

postgresql - 转到 time.Time 到 postgresql timestamptz 无法使用 time.Now() 保存

ruby-on-rails - Rails has_many 通过、分组和汇总数据

mysql - SQL Join 用于检索单个用户的记录

c# - 将 C# 类(类库)转换为 SQL DDL(表)

ruby-on-rails - Rails 预约网站 - 阻止某些时段/日期

ruby-on-rails - 从 Rails.application.routes.url_helpers 访问引擎路由