将连接计数与另一个连接列进行比较
Compare join count to another joined column
我想在
中想出我认为应该是一个非常简单的查询
相关机型:
一个Schedule
has_many
Events
,一个Event
has_many
attendees
。 我想获取所有 events
的 attendees
少于 event
的 schedule
的 min_attendees
列指定的内容.
SELECT "events".* FROM "events"
INNER JOIN "schedules" ON "schedules"."id" = "events"."schedule_id"
INNER JOIN "attendees" ON "attendees"."event_id" = "events"."id"
GROUP BY events.id
HAVING COUNT(attendees.id) < schedules.min_attendee
尝试上述查询时出现以下错误:
ERROR: column "schedules.min_attendees" must appear in the GROUP BY clause or be used in an aggregate function
您可以通过在其上使用聚合函数来解决此问题:
SELECT "events".*
FROM "events" INNER JOIN
"schedules"
ON "schedules"."id" = "events"."schedule_id" INNER JOIN
"attendees"
ON "attendees"."event_id" = "events"."id"
GROUP BY events.id
HAVING COUNT(attendees.id) < MIN(schedules.min_attendee)
或者,将其包含在 group by
:
SELECT "events".*
FROM "events" INNER JOIN
"schedules"
ON "schedules"."id" = "events"."schedule_id" INNER JOIN
"attendees"
ON "attendees"."event_id" = "events"."id"
GROUP BY events.id, schedules.min_attendee
HAVING COUNT(attendees.id) < schedules.min_attendee
如果您不将聚合函数放在 schedules.min_attendee 附近,那么它应该存在于 group by else 中,否则不需要。
这可能对你有帮助
SELECT "events".* FROM "events"
INNER JOIN "schedules" ON "schedules"."id" = "events"."schedule_id"
INNER JOIN "attendees" ON "attendees"."event_id" = "events"."id"
GROUP BY events.id
HAVING COUNT(attendees.id) < MAX(schedules.min_attendee)
您不能使用 MAX 或 MIN,因为每一行都会有所不同。
无论您使用 SELECT 的什么列都必须在 GROUP BY 子句中,否则它会抛出错误(它必须出现在 group by 子句或 agg f'n 中)
我想在
中想出我认为应该是一个非常简单的查询相关机型:
一个Schedule
has_many
Events
,一个Event
has_many
attendees
。 我想获取所有 events
的 attendees
少于 event
的 schedule
的 min_attendees
列指定的内容.
SELECT "events".* FROM "events"
INNER JOIN "schedules" ON "schedules"."id" = "events"."schedule_id"
INNER JOIN "attendees" ON "attendees"."event_id" = "events"."id"
GROUP BY events.id
HAVING COUNT(attendees.id) < schedules.min_attendee
尝试上述查询时出现以下错误:
ERROR: column "schedules.min_attendees" must appear in the GROUP BY clause or be used in an aggregate function
您可以通过在其上使用聚合函数来解决此问题:
SELECT "events".*
FROM "events" INNER JOIN
"schedules"
ON "schedules"."id" = "events"."schedule_id" INNER JOIN
"attendees"
ON "attendees"."event_id" = "events"."id"
GROUP BY events.id
HAVING COUNT(attendees.id) < MIN(schedules.min_attendee)
或者,将其包含在 group by
:
SELECT "events".*
FROM "events" INNER JOIN
"schedules"
ON "schedules"."id" = "events"."schedule_id" INNER JOIN
"attendees"
ON "attendees"."event_id" = "events"."id"
GROUP BY events.id, schedules.min_attendee
HAVING COUNT(attendees.id) < schedules.min_attendee
如果您不将聚合函数放在 schedules.min_attendee 附近,那么它应该存在于 group by else 中,否则不需要。
这可能对你有帮助
SELECT "events".* FROM "events"
INNER JOIN "schedules" ON "schedules"."id" = "events"."schedule_id"
INNER JOIN "attendees" ON "attendees"."event_id" = "events"."id"
GROUP BY events.id
HAVING COUNT(attendees.id) < MAX(schedules.min_attendee)
您不能使用 MAX 或 MIN,因为每一行都会有所不同。 无论您使用 SELECT 的什么列都必须在 GROUP BY 子句中,否则它会抛出错误(它必须出现在 group by 子句或 agg f'n 中)