过滤和分类
equals 等于
代码
查询 id 等于 4 的数据
ts
const result = await prisma.emp.findMany({
where: {
id: {
equals: 4,
},
},
});
console.log(result);
- 结果
js
[
{
id: 4,
name: '韦一笑',
age: 48,
job: '开发',
salary: 11000,
entrydate: 2002-02-05T00:00:00.000Z,
managerid: 2,
dept_id: 1
}
]
not 不等于
代码
查询 id 不等于 4 的数据
ts
const result = await prisma.emp.findMany({
where: {
id: {
not: 4,
},
},
});
console.log(result);
- 结果
bash
返回id不为4的所有数据
in
代码
- 查询 id 在这些数据中的数据
ts
const result = await prisma.emp.findMany({
where: {
id: {
in: [4, 5, 6],
},
},
});
console.log(result);
- 结果
bash
[
{
id: 4,
name: '韦一笑',
age: 48,
job: '开发',
salary: 11000,
entrydate: 2002-02-05T00:00:00.000Z,
managerid: 2,
dept_id: 1
},
{
id: 5,
name: '常遇春',
age: 43,
job: '开发',
salary: 10500,
entrydate: 2004-09-07T00:00:00.000Z,
managerid: 3,
dept_id: 1
},
{
id: 6,
name: '小昭',
age: 19,
job: '程序员鼓励师',
salary: 6600,
entrydate: 2004-10-12T00:00:00.000Z,
managerid: 2,
dept_id: 1
}
]
notIn
代码
- 查询 id 不在这些的数据中
ts
const result = await prisma.emp.findMany({
where: {
id: {
notIn: [2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17],
},
},
});
console.log(result);
- 结果
bash
[
{
id: 1,
name: '金庸',
age: 66,
job: '总裁',
salary: 20000,
entrydate: 2000-01-01T00:00:00.000Z,
managerid: null,
dept_id: 5
}
]
lt 小于
代码
查询 id 小于 2 的数据
ts
const result = await prisma.emp.findMany({
where: {
id: {
lt: 2,
},
},
});
console.log(result);
- 结果
ts
[
{
id: 1,
name: '金庸',
age: 66,
job: '总裁',
salary: 20000,
entrydate: 2000-01-01T00:00:00.000Z,
managerid: null,
dept_id: 5
}
]
lte 小于等于
代码
查询 id 小于等于 2 的数据
ts
const result = await prisma.emp.findMany({
where: {
id: {
lte: 2,
},
},
});
console.log(result);
- 结果
ts
[
{
id: 1,
name: '金庸',
age: 66,
job: '总裁',
salary: 20000,
entrydate: 2000-01-01T00:00:00.000Z,
managerid: null,
dept_id: 5
},
{
id: 2,
name: '张无忌',
age: 20,
job: '项目经理',
salary: 12500,
entrydate: 2005-12-05T00:00:00.000Z,
managerid: 1,
dept_id: 1
}
]
gt 大于
代码
查询 id 大于 16 的数据
ts
const result = await prisma.emp.findMany({
where: {
id: {
gt: 16,
},
},
});
console.log(result);
- 结果
bash
[
{
id: 17,
name: '陈友谅',
age: 42,
job: null,
salary: 2000,
entrydate: 2011-10-12T00:00:00.000Z,
managerid: 1,
dept_id: null
}
]
gte 大于等于
代码
查询 id 大于等于 16 的数据
ts
const result = await prisma.emp.findMany({
where: {
id: {
gte: 16,
},
},
});
console.log(result);
- 结果
bash
[
{
id: 16,
name: '宋远桥',
age: 40,
job: '销售',
salary: 4600,
entrydate: 2004-10-12T00:00:00.000Z,
managerid: 14,
dept_id: 4
},
{
id: 17,
name: '陈友谅',
age: 42,
job: null,
salary: 2000,
entrydate: 2011-10-12T00:00:00.000Z,
managerid: 1,
dept_id: null
}
]
contains 包含
代码
查询 name 包含 '张' 的数据
ts
const result = await prisma.emp.findMany({
where: {
name: {
contains: "张",
},
},
});
console.log(result);
- 结果
bash
[
{
id: 2,
name: '张无忌',
age: 20,
job: '项目经理',
salary: 12500,
entrydate: 2005-12-05T00:00:00.000Z,
managerid: 1,
dept_id: 1
},
{
id: 14,
name: '张三丰',
age: 88,
job: '销售总监',
salary: 14000,
entrydate: 2004-10-12T00:00:00.000Z,
managerid: 1,
dept_id: 4
}
]
startsWith 以 xx 开头搜索
代码
查询 name 以 张
开头的数据
ts
const result = await prisma.emp.findMany({
where: {
name: {
startsWith: "张",
},
},
});
console.log(result);
- 结果
bash
[
{
id: 2,
name: '张无忌',
age: 20,
job: '项目经理',
salary: 12500,
entrydate: 2005-12-05T00:00:00.000Z,
managerid: 1,
dept_id: 1
},
{
id: 14,
name: '张三丰',
age: 88,
job: '销售总监',
salary: 14000,
entrydate: 2004-10-12T00:00:00.000Z,
managerid: 1,
dept_id: 4
}
]
endssWith 以 xx 结尾的
代码
查询 name 以 丰
结尾的数据
ts
const result = await prisma.emp.findMany({
where: {
name: {
endsWith: "丰",
},
},
});
console.log(result);
- 结果
bash
[
{
id: 14,
name: '张三丰',
age: 88,
job: '销售总监',
salary: 14000,
entrydate: 2004-10-12T00:00:00.000Z,
managerid: 1,
dept_id: 4
}
]
AND
所有条件都必须返回 true。或者,将对象列表传递到 where 子句中,where 默认就是 AND
代码
查询名字包含张并且年龄>20 的数据
ts
const result = await prisma.emp.findMany({
where: {
AND: {
name: {
contains: "张",
},
age: {
gt: 20,
},
},
},
});
console.log(result);
- 返回结果
bash
[
{
id: 14,
name: '张三丰',
age: 88,
job: '销售总监',
salary: 14000,
entrydate: 2004-10-12T00:00:00.000Z,
managerid: 1,
dept_id: 4
}
]
不写 AND
ts
const result = await prisma.emp.findMany({
where: {
name: {
contains: "张",
},
age: {
gt: 20,
},
},
});
console.log(result);
- 返回结果
bash
[
{
id: 14,
name: '张三丰',
age: 88,
job: '销售总监',
salary: 14000,
entrydate: 2004-10-12T00:00:00.000Z,
managerid: 1,
dept_id: 4
}
]
OR
- 只能是个数组
只要有一个条件返回 true 就可以
代码
查询名字包含张或者年龄>60 的数据
ts
const result = await prisma.emp.findMany({
where: {
OR: [
{
name: {
contains: "张",
},
},
{
age: {
gt: 60,
},
},
],
},
});
console.log(result);
- 返回结果
js
[
{
id: 1,
name: '金庸',
age: 66,
job: '总裁',
salary: 20000,
entrydate: 2000-01-01T00:00:00.000Z,
managerid: null,
dept_id: 5
},
{
id: 2,
name: '张无忌',
age: 20,
job: '项目经理',
salary: 12500,
entrydate: 2005-12-05T00:00:00.000Z,
managerid: 1,
dept_id: 1
},
{
id: 14,
name: '张三丰',
age: 88,
job: '销售总监',
salary: 14000,
entrydate: 2004-10-12T00:00:00.000Z,
managerid: 1,
dept_id: 4
}
]
NOT
所有条件都必须返回 false
。
代码
找到年龄不小于 80 岁的
ts
const result = await prisma.emp.findMany({
where: {
NOT: {
age: {
lt: 80,
},
},
},
});
console.log(result);
- 返回结果
js
[
{
id: 14,
name: '张三丰',
age: 88,
job: '销售总监',
salary: 14000,
entrydate: 2004-10-12T00:00:00.000Z,
managerid: 1,
dept_id: 4
}
]
skip 和 take
- skip 跳过多少条数据
- take 取多少条数据 等价于 limit
代码
ts
const pageSize = 5; // 每页记录数
const pageNum = 2; // 当前页码
const results = await prisma.emp.findMany({
skip: (pageNum - 1) * pageSize, // 每次开始的位置
take: pageSize, // 每页返回的个数
});
console.log(results);
- 返回结果
每次改变 pageNum 即可