获取连接和分组查询中的总行数
Get total rows in joined and grouped query
我的 rails 应用程序中有一个相当复杂的查询,
我加入 table A 和 table B 这样我就可以彻底搜索两个 tables
然后,我在其 ID 上对 table A 进行分组,以删除由我在 table B 上的内部联接创建的重复项
然后我需要剩余记录的计数,以便我可以对结果进行分页。
但是这样做会在我尝试获取计数时出现以下异常:
ActiveRecord::StatementInvalid (PG::AmbiguousColumn: ERROR: column reference "game_id" is ambiguous
LINE 1: SELECT COUNT(*) AS count_all, game_id AS game_id, region AS ...
^
: SELECT COUNT(*) AS count_all, game_id AS game_id, region AS region FROM "game_data" INNER JOIN "player_participants" ON "player_participants"."game_id" = "game_data"."game_id" WHERE "player_participants"."region" = AND "game_data"."region" = 'NA1' AND "game_data"."status" = 1 GROUP BY "game_data"."game_id", "game_data"."region"):
这是我的 rails 代码:
games = GameDatum.joins(:player_participants).where(
player_participants: pp_data,
game_data: g_data
).group(:game_id).order(created_at: :desc).page(page).per(records)
games_json = {}
games_json[:pages] = games.total_pages
games.total_pages 使用 .count,这是给我带来问题的原因。
感谢您的帮助!
ERROR: column reference "game_id" is ambiguous
您的两个 table 中似乎都有 game_id
列。因此,请在查询中使用 table_name.column_name
约定。如果要使用 player_participants
table:
中的 game_id
,请使用 .group('player_participants.game_id')
而不是 group(:game_id)
games = GameDatum
.joins(:player_participants)
.where(player_participants: pp_data, game_data: g_data)
.group('player_participants.game_id')
.order(created_at: :desc)
.page(page)
.per(records)
从纯粹的 SQL 答案来看,问题是 game_id 出现在两个表中,因此您需要在查询中具体说明要使用哪个表,例如:
SELECT COUNT(*) AS count_all, "player_participants".game_id AS game_id, region AS region FROM "game_data" INNER JOIN "player_participants" ON "player_participants"."game_id" = "game_data"."game_id" WHERE "player_participants"."region" = AND "game_data"."region" = 'NA1' AND "game_data"."status" = 1 GROUP BY "game_data"."game_id", "game_data"."region"
因此我建议您尝试通过以下方式将 table.column_name 添加到您的组中:
.group('player_participants.game_id')
抱歉,我无法提供更多帮助。
我的 rails 应用程序中有一个相当复杂的查询, 我加入 table A 和 table B 这样我就可以彻底搜索两个 tables 然后,我在其 ID 上对 table A 进行分组,以删除由我在 table B 上的内部联接创建的重复项 然后我需要剩余记录的计数,以便我可以对结果进行分页。
但是这样做会在我尝试获取计数时出现以下异常:
ActiveRecord::StatementInvalid (PG::AmbiguousColumn: ERROR: column reference "game_id" is ambiguous
LINE 1: SELECT COUNT(*) AS count_all, game_id AS game_id, region AS ...
^
: SELECT COUNT(*) AS count_all, game_id AS game_id, region AS region FROM "game_data" INNER JOIN "player_participants" ON "player_participants"."game_id" = "game_data"."game_id" WHERE "player_participants"."region" = AND "game_data"."region" = 'NA1' AND "game_data"."status" = 1 GROUP BY "game_data"."game_id", "game_data"."region"):
这是我的 rails 代码:
games = GameDatum.joins(:player_participants).where(
player_participants: pp_data,
game_data: g_data
).group(:game_id).order(created_at: :desc).page(page).per(records)
games_json = {}
games_json[:pages] = games.total_pages
games.total_pages 使用 .count,这是给我带来问题的原因。
感谢您的帮助!
ERROR: column reference "game_id" is ambiguous
您的两个 table 中似乎都有 game_id
列。因此,请在查询中使用 table_name.column_name
约定。如果要使用 player_participants
table:
game_id
,请使用 .group('player_participants.game_id')
而不是 group(:game_id)
games = GameDatum
.joins(:player_participants)
.where(player_participants: pp_data, game_data: g_data)
.group('player_participants.game_id')
.order(created_at: :desc)
.page(page)
.per(records)
从纯粹的 SQL 答案来看,问题是 game_id 出现在两个表中,因此您需要在查询中具体说明要使用哪个表,例如:
SELECT COUNT(*) AS count_all, "player_participants".game_id AS game_id, region AS region FROM "game_data" INNER JOIN "player_participants" ON "player_participants"."game_id" = "game_data"."game_id" WHERE "player_participants"."region" = AND "game_data"."region" = 'NA1' AND "game_data"."status" = 1 GROUP BY "game_data"."game_id", "game_data"."region"
因此我建议您尝试通过以下方式将 table.column_name 添加到您的组中:
.group('player_participants.game_id')
抱歉,我无法提供更多帮助。