Prisma 客户端查询每个用户的最新值
Prisma client query for latest values of each user
我是 prisma-client
的新手,我想 return 给定公司的每个用户注册的最新值。
这是我的 schema.prisma
:
model Locations {
id Int @id @default(autoincrement())
user String
company String
latitude String
longitude String
timestamp String
}
这是我目前尝试过的方法:
const lastLocations = await db.locations.findMany({
where: {
company,
},
orderBy: {
id: 'desc',
},
take: 1,
});
但是我需要为每个用户获取 1 个值,我之前在 sql 中解决了这个问题:
WITH ranked_messages AS ( SELECT m.*, ROW_NUMBER() OVER (PARTITION BY userID ORDER BY timestamp DESC) AS rn FROM locations AS m ) SELECT * FROM ranked_messages WHERE rn = 1 AND companyID = "${companyID}";`;
但我不知道如何在 prisma 中进行。是否有“每个”方法?
感谢您的帮助。谢谢。
我使用 distinct
选项解决了:
const lastLocations = await db.locations.findMany({
where: {
company,
},
distinct: ['user'],
orderBy: {
id: 'desc',
},
});
谢谢。
我是 prisma-client
的新手,我想 return 给定公司的每个用户注册的最新值。
这是我的 schema.prisma
:
model Locations {
id Int @id @default(autoincrement())
user String
company String
latitude String
longitude String
timestamp String
}
这是我目前尝试过的方法:
const lastLocations = await db.locations.findMany({
where: {
company,
},
orderBy: {
id: 'desc',
},
take: 1,
});
但是我需要为每个用户获取 1 个值,我之前在 sql 中解决了这个问题:
WITH ranked_messages AS ( SELECT m.*, ROW_NUMBER() OVER (PARTITION BY userID ORDER BY timestamp DESC) AS rn FROM locations AS m ) SELECT * FROM ranked_messages WHERE rn = 1 AND companyID = "${companyID}";`;
但我不知道如何在 prisma 中进行。是否有“每个”方法?
感谢您的帮助。谢谢。
我使用 distinct
选项解决了:
const lastLocations = await db.locations.findMany({
where: {
company,
},
distinct: ['user'],
orderBy: {
id: 'desc',
},
});
谢谢。