在 Drizzle ORM 中,`.where()` 方法接受各种条件操作符来构建查询条件。以下是一些常用的条件操作符及其含义: 1. **eq**(等于) ```typescript import { eq } from "drizzle-orm"; db.select().from(table).where(eq(table.column, 5)); ``` ```sql SELECT * FROM table WHERE table.column = 5 ``` 用于检查列的值是否等于某个值 [(1)](https://orm.drizzle.team/docs/operators) 。 2. **ne**(不等于) ```typescript import { ne } from "drizzle-orm"; db.select().from(table).where(ne(table.column1, table.column2)); ``` ```sql SELECT * FROM table WHERE table.column1 <> table.column2 ``` 用于检查列的值是否不等于某个值。 3. **gt**(大于) ```typescript import { gt } from "drizzle-orm"; db.select().from(table).where(gt(table.column, 5)); ``` ```sql SELECT * FROM table WHERE table.column > 5 ``` 用于检查列的值是否大于某个值。 4. **gte**(大于等于) ```typescript import { gte } from "drizzle-orm"; db.select().from(table).where(gte(table.column, 5)); ``` ```sql SELECT * FROM table WHERE table.column >= 5 ``` 用于检查列的值是否大于或等于某个值。 5. **lt**(小于) ```typescript import { lt } from "drizzle-orm"; db.select().from(table).where(lt(table.column, 5)); ``` ```sql SELECT * FROM table WHERE table.column < 5 ``` 用于检查列的值是否小于某个值。 6. **lte**(小于等于) ```typescript import { lte } from "drizzle-orm"; db.select().from(table).where(lte(table.column, 5)); ``` ```sql SELECT * FROM table WHERE table.column <= 5 ``` 用于检查列的值是否小于或等于某个值。 7. **isNull**(为空) ```typescript import { isNull } from "drizzle-orm"; db.select().from(table).where(isNull(table.column)); ``` ```sql SELECT * FROM table WHERE table.column IS NULL ``` 用于检查列的值是否为 `NULL`。 8. **isNotNull**(不为空) ```typescript import { isNotNull } from "drizzle-orm"; db.select().from(table).where(isNotNull(table.column)); ``` ```sql SELECT * FROM table WHERE table.column IS NOT NULL ``` 用于检查列的值是否不为 `NULL`。 9. **inArray**(在数组中) ```typescript import { inArray } from "drizzle-orm"; db.select().from(table).where(inArray(table.column, [1, 2, 3, 4])); ``` ```sql SELECT * FROM table WHERE table.column IN (1, 2, 3, 4) ``` 用于检查列的值是否在给定的数组中。 10. **notInArray**(不在数组中) ```typescript import { notInArray } from "drizzle-orm"; db.select().from(table).where(notInArray(table.column, [1, 2, 3, 4])); ``` ```sql SELECT * FROM table WHERE table.column NOT IN (1, 2, 3, 4) ``` 用于检查列的值是否不在给定的数组中。 11. **exists**(存在) ```typescript import { exists } from "drizzle-orm"; const query = db.select().from(table2); db.select().from(table).where(exists(query)); ``` ```sql SELECT * FROM table WHERE EXISTS (SELECT * FROM table2) ``` 用于检查子查询是否返回任何行。 12. **notExists**(不存在) ```typescript import { notExists } from "drizzle-orm"; const query = db.select().from(table2); db.select().from(table).where(notExists(query)); ``` ```sql SELECT * FROM table WHERE NOT EXISTS (SELECT * FROM table2) ``` 用于检查子查询是否不返回任何行。 13. **between**(在范围之间) ```typescript import { between } from "drizzle-orm"; db.select().from(table).where(between(table.column, 2, 7)); ``` ```sql SELECT * FROM table WHERE table.column BETWEEN 2 AND 7 ``` 用于检查列的值是否在两个值之间。 14. **notBetween**(不在范围之间) ```typescript import { notBetween } from "drizzle-orm"; db.select().from(table).where(notBetween(table.column, 2, 7)); ``` ```sql SELECT * FROM table WHERE table.column NOT BETWEEN 2 AND 7 ``` 用于检查列的值是否不在两个值之间。 15. **like**(匹配模式) ```typescript import { like } from "drizzle-orm"; db.select().from(table).where(like(table.column, "%llo wor%")); ``` ```sql SELECT * FROM table WHERE table.column LIKE '%llo wor%' ``` 用于执行大小写敏感的模式匹配。 16. **ilike**(不区分大小写的模式匹配) ```typescript import { ilike } from "drizzle-orm"; db.select().from(table).where(ilike(table.column, "%llo wor%")); ``` ```sql SELECT * FROM table WHERE table.column ILIKE '%llo wor%' ``` 用于执行不区分大小写的模式匹配。 17. **notIlike**(不区分大小写的不匹配) ```typescript import { notIlike } from "drizzle-orm"; db.select().from(table).where(notIlike(table.column, "%llo wor%")); ``` ```sql SELECT * FROM table WHERE table.column NOT ILIKE '%llo wor%' ``` 用于执行不区分大小写的不匹配。 18. **not**(所有条件必须返回 `false`) ```typescript import { eq, not } from "drizzle-orm"; db.select().from(table).where(not(eq(table.column, 5))); ``` ```sql SELECT * FROM table WHERE NOT (table.column = 5) ``` 用于检查条件是否不满足。 19. **and**(所有条件必须返回 `true`) ```typescript import { gt, lt, and } from "drizzle-orm"; db.select().from(table).where(and(gt(table.column, 5), lt(table.column, 7))); ``` ```sql SELECT * FROM table WHERE (table.column > 5 AND table.column < 7) ``` 用于将多个条件组合在一起,所有条件必须满足。 20. **or**(一个或多个条件必须返回 `true`) ```typescript import { gt, lt, or } from "drizzle-orm"; db.select().from(table).where(or(gt(table.column, 5), lt(table.column, 7))); ``` ```sql SELECT * FROM table WHERE (table.column > 5 OR table.column < 7) ``` 用于将多个条件组合在一起,任意一个条件满足即可。 21. **arrayContains**(数组包含) ```typescript import { arrayContains } from "drizzle-orm"; const contains = await db.select({ id: posts.id }).from(posts) .where(arrayContains(posts.tags, ['Typescript', 'ORM'])); ``` ```sql select "id" from "posts" where "posts"."tags" @> {Typescript,ORM}; ``` 用于检查某列或表达式是否包含列表中的所有元素。 22. **arrayContained**(数组被包含) ```typescript import { arrayContained } from "drizzle-orm"; const contained = await db.select({ id: posts.id }).from(posts) .where(arrayContained(posts.tags, ['Typescript', 'ORM'])); ``` ```sql select "id" from "posts" where "posts"."tags" <@ {Typescript,ORM}; ``` 用于检查某列或表达式是否被列表中的所有元素包含。 23. **arrayOverlaps**(数组重叠) ```typescript import { arrayOverlaps } from "drizzle-orm"; const overlaps = await db.select({ id: posts.id }).from(posts) .where(arrayOverlaps(posts.tags, ['Typescript', 'ORM'])); ``` ```sql select "id" from "posts" where "posts"."tags" && {Typescript,ORM} ``` 用于检查某列或表达式是否与列表中的任何元素重叠。 这些条件操作符使得 Drizzle ORM 在构建复杂查询时非常灵活,能够满足各种不同的查询需求。 ### 引用: : [Filter and conditional operators](https://orm.drizzle.team/docs/operators)