SQL Select
Drizzle 在保持类型安全和可组合性的同时,为您提供了最接近 SQL 的方式从数据库中获取数据。
它本机支持几乎每个方言的查询功能和能力,
而它尚不支持的任何功能,都可以由用户通过强大的 sql
操作符添加。
对于以下示例,请假设您有一个像这样定义的 users
表:
基本和部分选择
选择所有列
选择包括所有列的表中的所有行:
请注意,结果类型是根据表定义自动推断的,包括列的可空性。
💡
Drizzle 总是在 select
子句中显式列出列,而不是使用 select *
。
内部必须这样做,以确保查询结果中字段的顺序,这也通常被认为是一种良好的实践。
部分选择
在某些情况下,您可能只想从表中选择部分列。
您可以通过向 .select()
方法提供一个选择对象来实现此目的:
与 SQL 一样,您可以使用任意表达式作为选择字段,而不仅仅是表列:
💡
通过指定 sql<string>
,你告诉 Drizzle 字段的期望类型是 string
。
如果你指定了错误的类型(例如,为将返回为字符串的字段使用 sql<number>
),运行时值将不匹配期望的类型。
Drizzle 无法根据提供的类型泛型执行任何类型转换,因为这些信息在运行时不可用。
如果需要对返回的值进行运行时转换,可以使用 .mapWith()
方法。
如果您经常使用表达式,可以将其提取到一个函数中:
条件选择
您可以根据某些条件拥有动态选择对象:
过滤
您可以使用 .where()
方法中的过滤操作器来过滤查询结果:
所有过滤操作符均使用 sql
函数实现。
您可以自己使用它来编写任意 SQL 过滤器或构建自己的操作符。
您可以参考 Drizzle 提供的操作符是如何实现的。
💡
提供给过滤操作符和 sql
函数的所有值都会自动参数化。
例如,这个查询:
将被转换为:
使用 not
操作符反转条件:
💡
您可以安全地更改模式、重命名表和列,由于模板插值的作用,这将自动反映在你的查询中,而不是在编写原始 SQL 时硬编码列或表的名称。
组合过滤器
您可以使用 and()
和 or()
操作符逻辑地组合过滤操作符:
Distinct
您可以使用 .selectDistinct()
来代替 .select()
,从数据集中仅检索唯一的行:
在 PostgreSQL 中,您还可以使用 distinct on
子句指定如何确定唯一行:
💡
distinct on
子句仅在 PostgreSQL 中受支持。
Limit & offset
使用 .limit()
和 .offset()
将 limit
和 offset
子句添加到查询中,例如用于实现分页:
Order By
使用 .orderBy()
将 order by
子句添加到查询中,按指定的字段对结果进行排序:
WITH 子句
使用 with
子句可以通过将复杂的查询拆分为更小的子查询(称为公共表达式)来简化查询:
要选择一个 CTE 中的任意 SQL 值作为字段,并在其他 CTE 或主查询中引用它们,
您需要为它们添加别名:
如果不提供别名,字段类型将变为 DrizzleTypeError
,您将无法在其他查询中引用它。
如果忽略类型错误并尝试仍然使用该字段,您将获得一个运行时错误,因为没有办法在没有别名的情况下引用该字段。
从子查询中选择
就像在 SQL 中一样,您可以使用子查询 API 将查询嵌入到其他查询中:
子查询可以在任何可以使用表的地方使用,例如在连接中:
迭代器
如果您需要从查询中返回非常大量的行,并且不希望将它们全部加载到内存中,可以使用 .iterator()
将查询转换为异步迭代器:
它也可以与 prepared statements 一起使用:
聚合
使用 Drizzle,您可以通过使用 sum
、count
、avg
等函数进行聚合,并通过 .groupBy()
和 .having()
进行分组和筛选,就像在原始 SQL 中一样:
💡
cast(... as int)
是必需的,因为 PostgreSQL 中 count()
返回 bigint
,MySQL 中返回 decimal
,它们被视为字符串值而不是数字。
或者,您可以使用 .mapWith(Number)
在运行时将值转换为数字。
聚合助手函数
Drizzle 提供了一组封装的 sql
函数,这样您就不需要为应用程序中的常见情况编写 sql
模板
💡
请记住,聚合函数通常与 SELECT 语句的 GROUP BY 子句一起使用。
因此,如果您正在使用聚合函数和其他列在一个查询中选择,
请务必使用 .groupBy
子句
count
返回 expression
中的值的数量。
countDistinct
返回 expression
中的非重复值的数量。
avg
返回 expression
中所有非空值的平均值(算术平均值)。
avgDistinct
返回 expression
中所有非空值的平均值(算术平均值)。
sum
返回 expression
中所有非空值的总和。
sumDistinct
返回 expression
中所有非空且不重复的值的总和。
max
返回 expression
中的最大值。
min
返回 expression
中的最小值。
更高级的示例: