Drizzle 总是在 select 子句中显式列出列,而不是使用 select *。
这在内部是必需的,以确保查询结果中的字段顺序,同时这通常也被认为是一种良好的实践。
SQL 查询
Drizzle 为您提供了最接近 SQL 的方式来从数据库中获取数据,同时保持类型安全和可组合性。
它原生支持几乎所有方言的查询特性和能力,
而它尚未支持的部分,也可以通过强大的 sql 操作符由用户自行添加。
对于下面的示例,我们假设您定义了一个 users 表,如下所示:
import { singlestoreTable, text, int } from 'drizzle-orm/singlestore-core';
export const users = singlestoreTable('users', {
id: int('id').primaryKey(),
name: text('name').notNull(),
age: int('age'),
});基础查询
从表中选择所有行,包括所有列:
const result = await db.select().from(users);
/*
{
id: number;
name: string;
age: number | null;
}[]
*/select `id`, `name`, `age` from `users`;注意,结果类型会根据表定义自动推断,包括列的可空性。
部分查询
在某些情况下,您可能只想从表中选择一部分列。
您可以通过向 .select() 方法提供一个选择对象来实现:
const result = await db.select({
field1: users.id,
field2: users.name,
}).from(users);
const { field1, field2 } = result[0];select `id`, `name` from `users`;和在 SQL 中一样,您可以将任意表达式用作选择字段,而不仅仅是表列:
const result = await db.select({
id: users.id,
lowerName: sql<string>`lower(${users.name})`,
}).from(users);select `id`, lower(`name`) from `users`;通过指定 sql<string>,您是在告诉 Drizzle 该字段的预期类型是 string。
如果您指定错误(例如,对一个运行时会以字符串返回的字段使用 sql<number>),运行时值将不会与预期类型匹配。
Drizzle 不能基于所提供的泛型类型执行任何类型转换,因为这些信息在运行时不可用。
如果您需要对返回值应用运行时转换,可以使用 .mapWith() 方法。
从 v1.0.0-beta.1 开始,您可以对列使用 .as():
const result = await db.select({
id: users.id,
lowerName: users.name.as("lower"),
}).from(users);条件查询
您可以基于某些条件构造动态选择对象:
async function selectUsers(withName: boolean) {
return db
.select({
id: users.id,
...(withName ? { name: users.name } : {}),
})
.from(users);
}
const users = await selectUsers(true);去重查询
您可以使用 .selectDistinct() 代替 .select(),以从数据集中仅检索唯一行:
await db.selectDistinct().from(users).orderBy(users.id, users.name);
await db.selectDistinct({ id: users.id }).from(users).orderBy(users.id);select distinct `id`, `name` from `users` order by `id`, `name`;
select distinct `id` from `users` order by `id`;高级查询
借助 TypeScript,Drizzle API 让您可以以多种灵活方式构建查询。
高级部分选择的预览——如需更详细的高级用法示例,请参阅我们的专门指南。
getColumns 从 drizzle-orm@1.0.0-beta.2 开始可用(更多信息请参见这里)
如果您使用的是 pre-1 版本(例如 0.45.1),请改用 getTableColumns
import { getColumns, sql } from 'drizzle-orm';
await db.select({
...getColumns(posts),
titleLength: sql<number>`length(${posts.title})`,
}).from(posts);---
筛选器
你可以在 .where() 方法中使用筛选运算符来过滤查询结果:
import { eq, lt, gte, ne } from 'drizzle-orm';
await db.select().from(users).where(eq(users.id, 42));
await db.select().from(users).where(lt(users.id, 42));
await db.select().from(users).where(gte(users.id, 42));
await db.select().from(users).where(ne(users.id, 42));
...select `id`, `name`, `age` from `users` where `id` = 42;
select `id`, `name`, `age` from `users` where `id` < 42;
select `id`, `name`, `age` from `users` where `id` >= 42;
select `id`, `name`, `age` from `users` where `id` <> 42;所有筛选运算符都是使用 sql 函数实现的。
你也可以自己使用它来编写任意 SQL 筛选条件,或者构建你自己的运算符。
作为参考,你可以查看 Drizzle 提供的这些运算符是如何实现的。
import { sql } from 'drizzle-orm';
function equals42(col: Column) {
return sql`${col} = 42`;
}
await db.select().from(users).where(sql`${users.id} < 42`);
await db.select().from(users).where(sql`${users.id} = 42`);
await db.select().from(users).where(equals42(users.id));
await db.select().from(users).where(sql`${users.id} >= 42`);
await db.select().from(users).where(sql`${users.id} <> 42`);
await db.select().from(users).where(sql`lower(${users.name}) = 'aaron'`);select `id`, `name`, `age` from `users` where `id` < 42;
select `id`, `name`, `age` from `users` where `id` = 42;
select `id`, `name`, `age` from `users` where `id` = 42;
select `id`, `name`, `age` from `users` where `id` >= 42;
select `id`, `name`, `age` from `users` where `id` <> 42;
select `id`, `name`, `age` from `users` where lower(`name`) = 'aaron';提供给筛选运算符和 sql 函数的所有值都会自动进行参数化。
例如,这个查询:
await db.select().from(users).where(eq(users.id, 42));将被翻译为:
select `id`, `name`, `age` from `users` where `id` = ?; -- params: [42]使用 not 运算符反转条件:
import { eq, not, sql } from 'drizzle-orm';
await db.select().from(users).where(not(eq(users.id, 42)));
await db.select().from(users).where(sql`not ${users.id} = 42`);select `id`, `name`, `age` from `users` where not (`id` = 42);
select `id`, `name`, `age` from `users` where not (`id` = 42);你可以安全地修改 schema、重命名表和列, 由于模板插值,这些变更会自动反映到你的查询中, 而不是在编写原始 SQL 时把列名或表名硬编码进去。
组合筛选器
你可以使用 and() 和 or() 运算符在逻辑上组合筛选运算符:
import { eq, and, sql } from 'drizzle-orm';
await db.select().from(users).where(
and(
eq(users.id, 42),
eq(users.name, 'Dan')
)
);
await db.select().from(users).where(sql`${users.id} = 42 and ${users.name} = 'Dan'`);select `id`, `name`, `age` from `users` where `id` = 42 and `name` = 'Dan';
select `id`, `name`, `age` from `users` where `id` = 42 and `name` = 'Dan';import { eq, or, sql } from 'drizzle-orm';
await db.select().from(users).where(
or(
eq(users.id, 42),
eq(users.name, 'Dan')
)
);
await db.select().from(users).where(sql`${users.id} = 42 or ${users.name} = 'Dan'`);select `id`, `name`, `age` from `users` where `id` = 42 or `name` = 'Dan';
select `id`, `name`, `age` from `users` where `id` = 42 or `name` = 'Dan';高级筛选器
结合 TypeScript,Drizzle API 为你提供了强大而灵活的方式来在查询中组合筛选条件。
条件筛选先睹为快;如需更详细的高级用法示例,请查看我们的专门指南。
const searchPosts = async (term?: string) => {
await db
.select()
.from(posts)
.where(term ? like(posts.title, term) : undefined);
};
await searchPosts();
await searchPosts('AI');---
按排序
使用 .orderBy() 为查询添加 order by 子句,按指定字段对结果排序:
import { asc, desc } from 'drizzle-orm';
await db.select().from(users).orderBy(users.name);
await db.select().from(users).orderBy(desc(users.name));
// 按多个字段排序
await db.select().from(users).orderBy(users.name, users.name2);
await db.select().from(users).orderBy(asc(users.name), desc(users.name2));select `id`, `name`, `age` from `users` order by `name`;
select `id`, `name`, `age` from `users` order by `name` desc;
select `id`, `name`, `age` from `users` order by `name`, `name2`;
select `id`, `name`, `age` from `users` order by `name` asc, `name2` desc;高级分页
借助 TypeScript,Drizzle API 让你能够实现所有可能的 SQL 分页和排序方式。
高级分页预览;如需更详细的高级用法示例,请参阅我们专门的 limit offset 分页 和 基于游标的分页 指南。
await db
.select()
.from(users)
.orderBy(asc(users.id)) // order by 是必需的
.limit(4) // 要返回的行数
.offset(4); // 要跳过的行数---
WITH 子句
使用 with 子句可以通过将复杂查询拆分为更小的子查询(称为公共表表达式,CTE)来帮助你简化它们:
const sq = db.$with('sq').as(db.select().from(users).where(eq(users.id, 42)));
const result = await db.with(sq).select().from(sq);with sq as (select `id`, `name`, `age` from `users` where `id` = 42)
select `id`, `name`, `age` from sq;SingleStore 不支持原生 RETURNING,因此在此方言中,依赖返回行的 insert、update 和 delete 语句不能在 with 中使用。
要在 CTE 中将任意 SQL 值作为字段进行选择,并在其他 CTE 或主查询中引用它们, 你需要为它们添加别名:
const sq = db.$with('sq').as(db.select({
name: sql<string>`upper(${users.name})`.as('name'),
})
.from(users));
const result = await db.with(sq).select({ name: sq.name }).from(sq);如果你不提供别名,字段类型将变为 DrizzleTypeError,并且你将无法在其他查询中引用它。
如果你忽略该类型错误并仍然尝试使用该字段,
你将得到一个运行时错误,因为在没有别名的情况下无法引用该字段。
从子查询中选择
就像在 SQL 中一样,你可以使用子查询 API 将查询嵌入到其他查询中:
const sq = db.select().from(users).where(eq(users.id, 42)).as('sq');
const result = await db.select().from(sq);select `id`, `name`, `age` from (select `id`, `name`, `age` from `users` where `id` = 42) `sq`;子查询可以在任何可以使用表的地方使用,例如在连接中:
const sq = db.select().from(users).where(eq(users.id, 42)).as('sq');
const result = await db.select().from(users).leftJoin(sq, eq(users.id, sq.id));select `users`.`id`, `users`.`name`, `users`.`age`, `sq`.`id`, `sq`.`name`, `sq`.`age` from `users`
left join (select `id`, `name`, `age` from `users` where `id` = 42) `sq`
on `users`.`id` = `sq`.`id`;---
聚合
使用 Drizzle,你可以像在原始 SQL 中一样,通过使用 sum、count、avg 等函数,并结合 .groupBy() 和 .having() 进行分组和过滤,来执行聚合:
import { gt } from 'drizzle-orm';
await db.select({
age: users.age,
count: sql<number>`cast(count(${users.id}) as int)`,
})
.from(users)
.groupBy(users.age);
await db.select({
age: users.age,
count: sql<number>`cast(count(${users.id}) as int)`,
})
.from(users)
.groupBy(users.age)
.having(({ count }) => gt(count, 1));select `age`, cast(count(`id`) as int)
from `users`
group by `age`;
select `age`, cast(count(`id`) as int)
from `users`
group by `age`
having cast(count(`id`) as int) > 1;cast(... as int) 是必要的,因为 count() 在 SingleStore 中返回 decimal,而它会被当作字符串值而不是数字。
或者,你也可以使用 .mapWith(Number) 在运行时将该值转换为数字。
如果你需要 count 聚合——我们推荐使用我们的 $count API
聚合辅助函数
Drizzle 提供了一组封装过的 sql 函数,因此在你的应用中遇到常见场景时,你无需编写 sql 模板
记住,聚合函数通常与 SELECT 语句的 GROUP BY 子句一起使用。
因此,如果你在一个查询中同时选择聚合函数和其他列,
请务必使用 .groupBy 子句
count
返回 expression 中值的数量。
import { count } from 'drizzle-orm'
await db.select({ value: count() }).from(users);
await db.select({ value: count(users.id) }).from(users);select count(*) from `users`;
select count(`id`) from `users`;// 它等价于编写
await db.select({
value: sql`count(*)`.mapWith(Number)
}).from(users);
await db.select({
value: sql`count(${users.id})`.mapWith(Number)
}).from(users);countDistinct
返回 expression 中非重复值的数量。
import { countDistinct } from 'drizzle-orm'
await db.select({ value: countDistinct(users.id) }).from(users);select count(distinct `id`) from `users`;// 它等价于编写
await db.select({
value: sql`count(${users.id})`.mapWith(Number)
}).from(users);avg
返回 expression 中所有非空值的平均值(算术平均数)。
import { avg } from 'drizzle-orm'
await db.select({ value: avg(users.id) }).from(users);select avg(`id`) from `users`;// 它等价于编写
await db.select({
value: sql`avg(${users.id})`.mapWith(String)
}).from(users);avgDistinct
返回 expression 中所有非空值的平均值(算术平均数)。
import { avgDistinct } from 'drizzle-orm'
await db.select({ value: avgDistinct(users.id) }).from(users);select avg(distinct `id`) from `users`;// 它等价于编写
await db.select({
value: sql`avg(distinct ${users.id})`.mapWith(String)
}).from(users);sum
返回 expression 中所有非空值的总和。
import { sum } from 'drizzle-orm'
await db.select({ value: sum(users.id) }).from(users);select sum(`id`) from `users`;// 它等价于编写
await db.select({
value: sql`sum(${users.id})`.mapWith(String)
}).from(users);sumDistinct
返回 expression 中所有非空且非重复值的总和。
import { sumDistinct } from 'drizzle-orm'
await db.select({ value: sumDistinct(users.id) }).from(users);select sum(distinct `id`) from `users`;// 它等价于编写
await db.select({
value: sql`sum(distinct ${users.id})`.mapWith(String)
}).from(users);max
返回 expression 中的最大值。
import { max } from 'drizzle-orm'
await db.select({ value: max(users.id) }).from(users);select max(`id`) from `users`;// 它等价于编写
await db.select({
value: sql`max(${expression})`.mapWith(users.id)
}).from(users);min
返回 expression 中的最小值。
import { min } from 'drizzle-orm'
await db.select({ value: min(users.id) }).from(users);select min(`id`) from `users`;// 它等价于编写
await db.select({
value: sql`min(${users.id})`.mapWith(users.id)
}).from(users);一个更高级的示例:
const orders = sqliteTable('order', {
id: integer('id').primaryKey(),
orderDate: integer('order_date', { mode: 'timestamp' }).notNull(),
requiredDate: integer('required_date', { mode: 'timestamp' }).notNull(),
shippedDate: integer('shipped_date', { mode: 'timestamp' }),
shipVia: integer('ship_via').notNull(),
freight: numeric('freight').notNull(),
shipName: text('ship_name').notNull(),
shipCity: text('ship_city').notNull(),
shipRegion: text('ship_region'),
shipPostalCode: text('ship_postal_code'),
shipCountry: text('ship_country').notNull(),
customerId: text('customer_id').notNull(),
employeeId: integer('employee_id').notNull(),
});
const details = sqliteTable('order_detail', {
unitPrice: numeric('unit_price').notNull(),
quantity: integer('quantity').notNull(),
discount: numeric('discount').notNull(),
orderId: integer('order_id').notNull(),
productId: integer('product_id').notNull(),
});
db
.select({
id: orders.id,
shippedDate: orders.shippedDate,
shipName: orders.shipName,
shipCity: orders.shipCity,
shipCountry: orders.shipCountry,
productsCount: sql<number>`cast(count(${details.productId}) as int)`,
quantitySum: sql<number>`sum(${details.quantity})`,
totalPrice: sql<number>`sum(${details.quantity} * ${details.unitPrice})`,
})
.from(orders)
.leftJoin(details, eq(orders.id, details.orderId))
.groupBy(orders.id)
.orderBy(asc(orders.id))
.all();$count
db.$count() 是 count(*) 的一个工具包装器,它是一个非常灵活的操作符,既可以直接使用,也可以作为子查询使用,更多详情请见我们的 GitHub 讨论。
const count = await db.$count(users);
// ^? number
const count = await db.$count(users, eq(users.name, "Dan")); // 与过滤条件一起使用select count(*) from "users";
select count(*) from "users" where "users"."name" = 'Dan';它在 子查询 中尤其有用:
const users = await db.select({
...users,
postsCount: db.$count(posts, eq(posts.authorId, users.id)),
}).from(users);关系查询 的使用示例
const users = await db.query.users.findMany({
extras: {
postsCount: (t) => db.$count(posts, eq(posts.authorId, t.id)),
},
});