Become a Gold Sponsor
Drizzle | SQL 增加值
PostgreSQL
MySQL
SQLite
This guide assumes familiarity with:

要增加列的值,可以使用 update().set() 方法,如下所示:

import { eq, sql } from 'drizzle-orm';

const db = drizzle(...)
  
await db
  .update(table)
  .set({
    counter: sql`${table.counter} + 1`,
  })
  .where(eq(table.id, 1));
update "table" set "counter" = "counter" + 1 where "id" = 1;

Drizzle 具有简单且灵活的 API,允许您轻松创建自定义解决方案。以下是如何创建自定义增量函数:

import { AnyColumn } from 'drizzle-orm';

const increment = (column: AnyColumn, value = 1) => {
  return sql`${column} + ${value}`;
};

await db
  .update(table)
  .set({
    counter1: increment(table.counter1),
    counter2: increment(table.counter2, 10),
  })
  .where(eq(table.id, 1));