聚合、分组和汇总
Prisma Client 允许您计数记录、聚合数字字段和选择不同的字段值。
聚合
Prisma Client 允许您对模型的数字字段(例如 Int
和 Float
)进行 aggregate
操作。以下查询返回所有用户的平均年龄
const aggregations = await prisma.user.aggregate({
_avg: {
age: true,
},
})
console.log('Average age:' + aggregations._avg.age)
您可以将聚合与过滤和排序结合使用。例如,以下查询返回用户的平均年龄
- 按
age
升序排序 - 其中
email
包含prisma.io
- 限制为 10 个用户
const aggregations = await prisma.user.aggregate({
_avg: {
age: true,
},
where: {
email: {
contains: 'prisma.io',
},
},
orderBy: {
age: 'asc',
},
take: 10,
})
console.log('Average age:' + aggregations._avg.age)
聚合值可为空
在 2.21.0 及更高版本中,对可为空字段的聚合可以返回 number
或 null
。这不包括 count
,如果未找到记录,它始终返回 0。
考虑以下查询,其中 age
在 schema 中是可为空的
const aggregations = await prisma.user.aggregate({
_avg: {
age: true,
},
_count: {
age: true,
},
})
{
_avg: {
age: null
},
_count: {
age: 9
}
}
在以下任一情况下,查询都会返回 { _avg: { age: null } }
- 没有用户
- 每个用户的
age
字段的值都是null
这允许您区分真实聚合值(可能为零)和无数据。
分组
Prisma Client 的 groupBy()
允许您按一个或多个字段值(例如 country
,或 country
和 city
)对记录进行分组,并对每个组执行聚合,例如查找居住在特定城市的人的平均年龄。groupBy()
在 2.20.0 及更高版本中是 GA。
以下视频使用 groupBy()
汇总了按洲划分的 COVID-19 总病例数
以下示例按 country
字段对所有用户进行分组,并返回每个国家/地区的个人资料浏览总数
const groupUsers = await prisma.user.groupBy({
by: ['country'],
_sum: {
profileViews: true,
},
})
如果您的 by
选项中只有一个元素,则可以使用以下简写语法来表达您的查询
const groupUsers = await prisma.user.groupBy({
by: 'country',
})
groupBy()
和过滤
groupBy()
支持两个级别的过滤:where
和 having
。
使用 where
过滤记录
使用 where
在分组之前过滤所有记录。以下示例按国家/地区对用户进行分组并汇总个人资料浏览量,但仅包括电子邮件地址包含 prisma.io
的用户
const groupUsers = await prisma.user.groupBy({
by: ['country'],
where: {
email: {
contains: 'prisma.io',
},
},
_sum: {
profileViews: true,
},
})
使用 having
过滤组
使用 having
按聚合值(例如字段的总和或平均值,而不是单个记录)过滤整个组 - 例如,仅返回平均 profileViews
大于 100 的组
const groupUsers = await prisma.user.groupBy({
by: ['country'],
where: {
email: {
contains: 'prisma.io',
},
},
_sum: {
profileViews: true,
},
having: {
profileViews: {
_avg: {
gt: 100,
},
},
},
})
having
的用例
having
的主要用例是对聚合进行过滤。我们建议您尽可能在分组之前使用 where
来减小数据集的大小,因为这样做 ✔ 减少了数据库必须返回的记录数,并且 ✔ 利用了索引。
例如,以下查询对所有非来自瑞典或加纳的用户进行分组
const fd = await prisma.user.groupBy({
by: ['country'],
where: {
country: {
notIn: ['Sweden', 'Ghana'],
},
},
_sum: {
profileViews: true,
},
having: {
profileViews: {
_min: {
gte: 10,
},
},
},
})
以下查询在技术上实现了相同的结果,但在分组后排除了来自加纳的用户。这没有任何好处,并且不推荐使用。
const groupUsers = await prisma.user.groupBy({
by: ['country'],
where: {
country: {
not: 'Sweden',
},
},
_sum: {
profileViews: true,
},
having: {
country: {
not: 'Ghana',
},
profileViews: {
_min: {
gte: 10,
},
},
},
})
注意:在
having
中,您只能对聚合值或by
中可用的字段进行过滤。
groupBy()
和排序
当您组合 groupBy()
和 orderBy
时,以下约束适用
- 您可以
orderBy
by
中存在的字段 - 您可以
orderBy
聚合(在 2.21.0 及更高版本中预览) - 如果您将
skip
和/或take
与groupBy()
一起使用,则还必须在查询中包含orderBy
按聚合组排序
您可以按聚合组排序。Prisma ORM 在版本 2.21.0 中为关系数据库添加了对将 orderBy
与聚合组一起使用的支持,并在 3.4.0 中为 MongoDB 添加了支持。
以下示例按每个 city
组中用户的数量对组进行排序(最大的组排在最前面)
const groupBy = await prisma.user.groupBy({
by: ['city'],
_count: {
city: true,
},
orderBy: {
_count: {
city: 'desc',
},
},
})
按字段排序
以下查询按国家/地区对组进行排序,跳过前两个组,并返回第 3 和第 4 个组
const groupBy = await prisma.user.groupBy({
by: ['country'],
_sum: {
profileViews: true,
},
orderBy: {
country: 'desc',
},
skip: 2,
take: 2,
})
groupBy()
常见问题解答
我可以在 groupBy()
中使用 select
吗?
您不能在 groupBy()
中使用 select
。但是,by
中包含的所有字段都会自动返回。
在 groupBy()
中使用 where
和 having
有什么区别?
where
在分组之前过滤所有记录,而 having
过滤整个组,并支持对聚合字段值进行过滤,例如该组中特定字段的平均值或总和。
groupBy()
和 distinct
之间有什么区别?
distinct
和 groupBy()
都按一个或多个唯一字段值对记录进行分组。groupBy()
允许您聚合每个组内的数据 - 例如,返回丹麦帖子的平均浏览量 - 而 distinct 不允许。
计数
计数记录
使用 count()
来计数记录数或非 null
字段值。以下示例查询计数所有用户
const userCount = await prisma.user.count()
计数关系
要返回关系计数(例如,用户的帖子计数),请将 _count
参数与嵌套的 select
一起使用,如下所示
const usersWithCount = await prisma.user.findMany({
include: {
_count: {
select: { posts: true },
},
},
})
_count
参数
- 可以在顶层
include
或select
中使用 - 可以与任何返回记录的查询一起使用(包括
delete
、update
和findFirst
) - 可以返回多个关系计数
- 可以过滤关系计数(从 4.3.0 版本开始)
使用 include
返回关系计数
以下查询在结果中包含每个用户的帖子计数
const usersWithCount = await prisma.user.findMany({
include: {
_count: {
select: { posts: true },
},
},
})
使用 select
返回关系计数
以下查询使用 select
返回每个用户的帖子计数并且不返回其他字段
const usersWithCount = await prisma.user.findMany({
select: {
_count: {
select: { posts: true },
},
},
})
返回多个关系计数
以下查询返回每个用户的 posts
和 recipes
的计数,并且不返回其他字段
const usersWithCount = await prisma.user.findMany({
select: {
_count: {
select: {
posts: true,
recipes: true,
},
},
},
})
过滤关系计数
使用 where
过滤 _count
输出类型返回的字段。您可以在 标量字段、关系字段 和 复合类型 的字段上执行此操作。
例如,以下查询返回标题为“Hello!”的所有用户帖子
// Count all user posts with the title "Hello!"
await prisma.user.findMany({
select: {
_count: {
select: {
posts: { where: { title: 'Hello!' } },
},
},
},
})
以下查询查找来自名为“Alice”的作者的评论的所有用户帖子
// Count all user posts that have comments
// whose author is named "Alice"
await prisma.user.findMany({
select: {
_count: {
select: {
posts: {
where: { comments: { some: { author: { is: { name: 'Alice' } } } } },
},
},
},
},
})
计数非 null
字段值
在 2.15.0 及更高版本中,您可以计数所有记录以及所有非 null
字段值的实例。以下查询返回计数
- 所有
User
记录 (_all
) - 所有非
null
name
值(不是不同的值,只是非null
的值)
const userCount = await prisma.user.count({
select: {
_all: true, // Count all records
name: true, // Count all non-null field values
},
})
过滤计数
count
支持过滤。以下示例查询计数个人资料浏览量超过 100 的所有用户
const userCount = await prisma.user.count({
where: {
profileViews: {
gte: 100,
},
},
})
以下示例查询计数特定用户的帖子
const postCount = await prisma.post.count({
where: {
authorId: 29,
},
})
选择不同的值
Prisma Client 允许您使用 distinct
从 Prisma 查询响应中过滤重复行到 findMany
查询。 distinct
通常与 select
结合使用,以识别表中行中某些唯一的值组合。
以下示例返回具有不同 name
字段值的所有 User
记录的所有字段
const result = await prisma.user.findMany({
where: {},
distinct: ['name'],
})
以下示例返回不同的 role
字段值(例如,ADMIN
和 USER
)
const distinctRoles = await prisma.user.findMany({
distinct: ['role'],
select: {
role: true,
},
})
distinct
底层原理
Prisma Client 的 distinct
选项不使用 SQL SELECT DISTINCT
。相反,distinct
使用
- 一个
SELECT
查询 - 内存后处理以选择不同的值
它以这种方式设计是为了支持 select
和 include
作为 distinct
查询的一部分。
以下示例选择 gameId
和 playerId
上的不同值,按 score
排序,以便返回每个玩家在每个游戏中获得的最高分。该查询使用 include
和 select
来包含其他数据
- 选择
score
(Play
上的字段) - 选择相关玩家姓名 (
Play
和User
之间的关系) - 选择相关游戏名称 (
Play
和Game
之间的关系)
展开以查看示例 schema
model User {
id Int @id @default(autoincrement())
name String?
play Play[]
}
model Game {
id Int @id @default(autoincrement())
name String?
play Play[]
}
model Play {
id Int @id @default(autoincrement())
score Int? @default(0)
playerId Int?
player User? @relation(fields: [playerId], references: [id])
gameId Int?
game Game? @relation(fields: [gameId], references: [id])
}
const distinctScores = await prisma.play.findMany({
distinct: ['playerId', 'gameId'],
orderBy: {
score: 'desc',
},
select: {
score: true,
game: {
select: {
name: true,
},
},
player: {
select: {
name: true,
},
},
},
})
如果没有 select
和 distinct
,查询将返回
[
{
gameId: 2,
playerId: 5
},
{
gameId: 2,
playerId: 10
}
]