通过指定 sql<number>,您告诉 Drizzle 字段的 预期 类型是 number。
如果您指定错误(例如,对将以数字返回的字段使用 sql<string>),运行时值将不匹配预期类型。
Drizzle 不能根据提供的类型泛型执行任何类型转换,因为该信息在运行时不可用。
如果您需要对返回值应用运行时转换,可以使用 .mapWith() 方法。
要统计表中的所有行,可以使用 count() 函数或 sql 操作符,如下所示:
import { count, sql } from 'drizzle-orm';
import { products } from './schema';
const db = drizzle(...);
await db.select({ count: count() }).from(products);
// 在底层,count() 函数会在运行时将其结果转换为数字。
await db.select({ count: sql`count(*)`.mapWith(Number) }).from(products);// 结果类型
type Result = {
count: number;
}[];select count(*) from products;要统计指定列包含非 NULL 值的行,可以使用带有列的 count() 函数:
await db.select({ count: count(products.discount) }).from(products);// 结果类型
type Result = {
count: number;
}[];select count("discount") from products;Drizzle 拥有简单灵活的 API,可以让您创建自定义解决方案。在 PostgreSQL 和 MySQL 中,count() 函数返回 bigint,该值由其驱动程序解释为字符串,因此应转换为整数:
import { AnyColumn, sql } from 'drizzle-orm';
const customCount = (column?: AnyColumn) => {
if (column) {
return sql<number>`cast(count(${column}) as integer)`; // 在 MySQL 中转换为无符号整数
} else {
return sql<number>`cast(count(*) as integer)`; // 在 MySQL 中转换为无符号整数
}
};
await db.select({ count: customCount() }).from(products);
await db.select({ count: customCount(products.discount) }).from(products);select cast(count(*) as integer) from products;
select cast(count("discount") as integer) from products;在 SQLite 中,count() 的结果作为整数返回。
import { sql } from 'drizzle-orm';
await db.select({ count: sql<number>`count(*)` }).from(products);
await db.select({ count: sql<number>`count(${products.discount})` }).from(products);select count(*) from products;
select count("discount") from products;通过指定 sql<number>,您告诉 Drizzle 字段的 预期 类型是 number。
如果您指定错误(例如,对将以数字返回的字段使用 sql<string>),运行时值将不匹配预期类型。
Drizzle 不能根据提供的类型泛型执行任何类型转换,因为该信息在运行时不可用。
如果您需要对返回值应用运行时转换,可以使用 .mapWith() 方法。
要统计匹配条件的行,可以使用 .where() 方法:
import { count, gt } from 'drizzle-orm';
await db
.select({ count: count() })
.from(products)
.where(gt(products.price, 100));select count(*) from products where price > 100以下是如何使用 count() 函数结合连接和聚合:
import { count, eq } from 'drizzle-orm';
import { countries, cities } from './schema';
// 统计每个国家的城市数量
await db
.select({
country: countries.name,
citiesCount: count(cities.id),
})
.from(countries)
.leftJoin(cities, eq(countries.id, cities.countryId))
.groupBy(countries.id)
.orderBy(countries.name);select countries.name, count("cities"."id") from countries
left join cities on countries.id = cities.country_id
group by countries.id
order by countries.name;