集合操作
SQL 集合操作将多个查询块的结果合并为一个结果。SQL 标准定义了以下三种集合操作:UNION
(并集)、INTERSECT
(交集)、EXCEPT
(差集),以及它们的 ALL
版本:UNION ALL
(并集所有)、INTERSECT ALL
(交集所有)、EXCEPT ALL
(差集所有)。
并集
将两个查询块的所有结果合并为一个结果,并省略所有重复项。
从 customers 表和 users 表中获取所有名称,不含重复项。
import-pattern
builder-pattern
schema.ts
import { union } from 'drizzle-orm/pg-core'
import { users, customers } from './schema'
const allNamesForUserQuery = db.select({ name: users.name }).from(users);
const result = await union(
allNamesForUserQuery,
db.select({ name: customers.name }).from(customers)
).limit(10);
(select "name" from "sellers")
union
(select "name" from "customers")
limit $1
import { users, customers } from './schema'
const result = await db
.select({ name: users.name })
.from(users)
.union(db.select({ name: customers.name }).from(customers))
.limit(10);
(select "name" from "sellers")
union
(select "name" from "customers")
limit $1
import { integer, pgTable, text, varchar } from "drizzle-orm/pg-core";
const users = pgTable('sellers', {
id: integer('id').primaryKey(),
name: varchar('name', { length: 256 }).notNull(),
address: text('address'),
});
const customers = pgTable('customers', {
id: integer('id').primaryKey(),
name: varchar('name', { length: 256 }).notNull(),
city: text('city'),
email: varchar('email', { length: 256 }).notNull()
});
import-pattern
builder-pattern
schema.ts
import { union } from 'drizzle-orm/mysql-core'
import { users, customers } from './schema'
const allNamesForUserQuery = db.select({ name: users.name }).from(users);
const result = await union(
allNamesForUserQuery,
db.select({ name: customers.name }).from(customers)
).limit(10);
(select `name` from `sellers`)
union
(select `name` from `customers`)
limit ?
import { users, customers } from './schema'
const result = await db
.select({ name: users.name })
.from(users)
.union(db.select({ name: customers.name }).from(customers))
.limit(10);
(select `name` from `sellers`)
union
(select `name` from `customers`)
limit ?
import { int, mysqlTable, text, varchar } from "drizzle-orm/mysql-core";
const users = mysqlTable('sellers', {
id: int('id').primaryKey(),
name: varchar('name', { length: 256 }).notNull(),
address: text('address'),
});
const customers = mysqlTable('customers', {
id: int('id').primaryKey(),
name: varchar('name', { length: 256 }).notNull(),
city: text('city'),
email: varchar('email', { length: 256 }).notNull()
});
import-pattern
builder-pattern
schema.ts
import { union } from 'drizzle-orm/sqlite-core'
import { users, customers } from './schema'
const allNamesForUserQuery = db.select({ name: users.name }).from(users);
const result = await union(
allNamesForUserQuery,
db.select({ name: customers.name }).from(customers)
).limit(10);
(select "name" from "sellers")
union
(select "name" from "customers")
limit ?
import { users, customers } from './schema'
const result = await db
.select({ name: users.name })
.from(users)
.union(db.select({ name: customers.name }).from(customers))
.limit(10);
select "name" from "sellers" union select "name" from "customers" limit ?
import { int, sqliteTable, text } from "drizzle-orm/sqlite-core";
const users = sqliteTable('sellers', {
id: int('id').primaryKey(),
name: text('name').notNull(),
address: text('address'),
});
const customers = sqliteTable('customers', {
id: int('id').primaryKey(),
name: text('name').notNull(),
city: text('city'),
email: text('email').notNull()
});
import-pattern
builder-pattern
schema.ts
import { union } from 'drizzle-orm/singlestore-core'
import { users, customers } from './schema'
const allNamesForUserQuery = db.select({ name: users.name }).from(users);
const result = await union(
allNamesForUserQuery,
db.select({ name: customers.name }).from(customers)
).limit(10);
(select `name` from `sellers`)
union
(select `name` from `customers`)
limit ?
import { users, customers } from './schema'
const result = await db
.select({ name: users.name })
.from(users)
.union(db.select({ name: customers.name }).from(customers))
.limit(10);
(select `name` from `sellers`)
union
(select `name` from `customers`)
limit ?
import { int, mysqlTable, text, varchar } from "drizzle-orm/singlestore-core";
const users = mysqlTable('sellers', {
id: int('id').primaryKey(),
name: varchar('name', { length: 256 }).notNull(),
address: text('address'),
});
const customers = mysqlTable('customers', {
id: int('id').primaryKey(),
name: varchar('name', { length: 256 }).notNull(),
city: text('city'),
email: varchar('email', { length: 256 }).notNull()
});
并集所有
将两个查询块的所有结果合并为一个结果,包含重复项。
假设您有两个表,一个表示线上销售,另一个表示店内销售。在这种情况下,您希望将两个表中的数据合并到一个结果集中。由于可能存在重复交易,您希望保留所有记录而不消除重复项。
import-pattern
builder-pattern
schema.ts
import { unionAll } from 'drizzle-orm/pg-core'
import { onlineSales, inStoreSales } from './schema'
const onlineTransactions = db.select({ transaction: onlineSales.transactionId }).from(onlineSales);
const inStoreTransactions = db.select({ transaction: inStoreSales.transactionId }).from(inStoreSales);
const result = await unionAll(onlineTransactions, inStoreTransactions);
select "transaction_id" from "online_sales"
union all
select "transaction_id" from "in_store_sales"
import { onlineSales, inStoreSales } from './schema'
const result = await db
.select({ transaction: onlineSales.transactionId })
.from(onlineSales)
.unionAll(
db.select({ transaction: inStoreSales.transactionId }).from(inStoreSales)
);
select "transaction_id" from "online_sales"
union all
select "transaction_id" from "in_store_sales"
import { integer, pgTable, text, timestamp, varchar } from "drizzle-orm/pg-core";
const onlineSales = pgTable('online_sales', {
transactionId: integer('transaction_id').primaryKey(),
productId: integer('product_id').unique(),
quantitySold: integer('quantity_sold'),
saleDate: timestamp('sale_date', { mode: 'date' }),
});
const inStoreSales = pgTable('in_store_sales', {
transactionId: integer('transaction_id').primaryKey(),
productId: integer('product_id').unique(),
quantitySold: integer('quantity_sold'),
saleDate: timestamp('sale_date', { mode: 'date' }),
});
import-pattern
builder-pattern
schema.ts
import { unionAll } from 'drizzle-orm/mysql-core'
import { onlineSales, inStoreSales } from './schema'
const onlineTransactions = db.select({ transaction: onlineSales.transactionId }).from(onlineSales);
const inStoreTransactions = db.select({ transaction: inStoreSales.transactionId }).from(inStoreSales);
const result = await unionAll(onlineTransactions, inStoreTransactions);
select `transaction_id` from `online_sales`
union all
select `transaction_id` from `in_store_sales`
import { onlineSales, inStoreSales } from './schema'
const result = await db
.select({ transaction: onlineSales.transactionId })
.from(onlineSales)
.unionAll(
db.select({ transaction: inStoreSales.transactionId }).from(inStoreSales)
);
(select `transaction_id` from `online_sales`)
union all
(select `transaction_id` from `in_store_sales`)
import { int, mysqlTable, text, timestamp, varchar } from "drizzle-orm/mysql-core";
const onlineSales = mysqlTable('online_sales', {
transactionId: int('transaction_id').primaryKey(),
productId: int('product_id').unique(),
quantitySold: int('quantity_sold'),
saleDate: timestamp('sale_date', { mode: 'date' }),
});
const inStoreSales = mysqlTable('in_store_sales', {
transactionId: int('transaction_id').primaryKey(),
productId: int('product_id').unique(),
quantitySold: int('quantity_sold'),
saleDate: timestamp('sale_date', { mode: 'date' }),
});
import-pattern
builder-pattern
schema.ts
import { unionAll } from 'drizzle-orm/sqlite-core'
import { onlineSales, inStoreSales } from './schema'
const onlineTransactions = db.select({ transaction: onlineSales.transactionId }).from(onlineSales);
const inStoreTransactions = db.select({ transaction: inStoreSales.transactionId }).from(inStoreSales);
const result = await unionAll(onlineTransactions, inStoreTransactions);
select "transaction_id" from "online_sales"
union all
select "transaction_id" from "in_store_sales"
import { onlineSales, inStoreSales } from './schema'
const result = await db
.select({ transaction: onlineSales.transactionId })
.from(onlineSales)
.unionAll(
db.select({ transaction: inStoreSales.transactionId }).from(inStoreSales)
);
select "transaction_id" from "online_sales"
union all
select "transaction_id" from "in_store_sales"
import { int, sqliteTable } from "drizzle-orm/sqlite-core";
const onlineSales = sqliteTable('online_sales', {
transactionId: int('transaction_id').primaryKey(),
productId: int('product_id').unique(),
quantitySold: int('quantity_sold'),
saleDate: int('sale_date', { mode: 'timestamp' }),
});
const inStoreSales = sqliteTable('in_store_sales', {
transactionId: int('transaction_id').primaryKey(),
productId: int('product_id').unique(),
quantitySold: int('quantity_sold'),
saleDate: int('sale_date', { mode: 'timestamp' }),
});
重要提示
UNION ALL 与 ORDER BY 行为与 MySQL 不一致:SingleStore 解析 UNION ALL 后跟 ORDER BY 命令的方式与 MySQL 不同。在 SingleStore 中,以下查询是有效的。但在 MySQL 中,它是无效的。
import-pattern
builder-pattern
schema.ts
import { unionAll } from 'drizzle-orm/singlestore-core'
import { onlineSales, inStoreSales } from './schema'
const onlineTransactions = db.select({ transaction: onlineSales.transactionId }).from(onlineSales);
const inStoreTransactions = db.select({ transaction: inStoreSales.transactionId }).from(inStoreSales);
const result = await unionAll(onlineTransactions, inStoreTransactions);
select `transaction_id` from `online_sales`
union all
select `transaction_id` from `in_store_sales`
import { onlineSales, inStoreSales } from './schema'
const result = await db
.select({ transaction: onlineSales.transactionId })
.from(onlineSales)
.unionAll(
db.select({ transaction: inStoreSales.transactionId }).from(inStoreSales)
);
(select `transaction_id` from `online_sales`)
union all
(select `transaction_id` from `in_store_sales`)
import { int, mysqlTable, text, timestamp, varchar } from "drizzle-orm/singlestore-core";
const onlineSales = mysqlTable('online_sales', {
transactionId: int('transaction_id').primaryKey(),
productId: int('product_id').unique(),
quantitySold: int('quantity_sold'),
saleDate: timestamp('sale_date', { mode: 'date' }),
});
const inStoreSales = mysqlTable('in_store_sales', {
transactionId: int('transaction_id').primaryKey(),
productId: int('product_id').unique(),
quantitySold: int('quantity_sold'),
saleDate: timestamp('sale_date', { mode: 'date' }),
});
交集
只合并两个查询块结果中共同的行,并省略所有重复项。
假设您有两个表,存储了学生课程注册信息。您想找出两个不同系之间共同的课程,但您只想要唯一的课程名称,并且不关心同一学生多次注册同一课程的情况。
在这种情况下,您想找出两个系之间共同的课程,但即使同一系有多个学生注册了同一课程,您也不想多次计数同一课程。
import-pattern
builder-pattern
schema.ts
import { intersect } from 'drizzle-orm/pg-core'
import { depA, depB } from './schema'
const departmentACourses = db.select({ courseName: depA.courseName }).from(depA);
const departmentBCourses = db.select({ courseName: depB.courseName }).from(depB);
const result = await intersect(departmentACourses, departmentBCourses);
select "course_name" from "department_a_courses"
intersect
select "course_name" from "department_b_courses"
import { depA, depB } from './schema'
const result = await db
.select({ courseName: depA.courseName })
.from(depA)
.intersect(db.select({ courseName: depB.courseName }).from(depB));
select "course_name" from "department_a_courses"
intersect
select "course_name" from "department_b_courses"
import { integer, pgTable, varchar } from "drizzle-orm/pg-core";
const depA = pgTable('department_a_courses', {
studentId: integer('student_id'),
courseName: varchar('course_name').notNull(),
});
const depB = pgTable('department_b_courses', {
studentId: integer('student_id'),
courseName: varchar('course_name').notNull(),
});
import-pattern
builder-pattern
schema.ts
import { intersect } from 'drizzle-orm/mysql-core'
import { depA, depB } from './schema'
const departmentACourses = db.select({ courseName: depA.courseName }).from(depA);
const departmentBCourses = db.select({ courseName: depB.courseName }).from(depB);
const result = await intersect(departmentACourses, departmentBCourses);
select `projects_name` from `department_a_projects`
intersect
select `projects_name` from `department_b_projects`
import { depA, depB } from './schema'
const result = await db
.select({ courseName: depA.courseName })
.from(depA)
.intersect(db.select({ courseName: depB.courseName }).from(depB));
select `projects_name` from `department_a_projects`
intersect
select `projects_name` from `department_b_projects`
import { int, mysqlTable, varchar } from "drizzle-orm/mysql-core";
const depA = mysqlTable('department_a_courses', {
studentId: int('student_id'),
courseName: varchar('course_name', { length: 256 }).notNull(),
});
const depB = pgTable('department_b_courses', {
studentId: int('student_id'),
courseName: varchar('course_name', { length: 256 }).notNull(),
});
import-pattern
builder-pattern
schema.ts
import { intersect } from 'drizzle-orm/sqlite-core'
import { depA, depB } from './schema'
const departmentACourses = db.select({ courseName: depA.courseName }).from(depA);
const departmentBCourses = db.select({ courseName: depB.courseName }).from(depB);
const result = await intersect(departmentACourses, departmentBCourses);
select "course_name" from "department_a_courses"
intersect
select "course_name" from "department_b_courses"
import { depA, depB } from './schema'
const result = await db
.select({ courseName: depA.courseName })
.from(depA)
.intersect(db.select({ courseName: depB.courseName }).from(depB));
select "course_name" from "department_a_courses"
intersect
select "course_name" from "department_b_courses"
import { int, sqliteTable, text } from "drizzle-orm/sqlite-core";
const depA = sqliteTable('department_a_courses', {
studentId: int('student_id'),
courseName: text('course_name').notNull(),
});
const depB = sqliteTable('department_b_courses', {
studentId: int('student_id'),
courseName: text('course_name').notNull(),
});
import-pattern
builder-pattern
schema.ts
import { intersect } from 'drizzle-orm/singlestore-core'
import { depA, depB } from './schema'
const departmentACourses = db.select({ courseName: depA.courseName }).from(depA);
const departmentBCourses = db.select({ courseName: depB.courseName }).from(depB);
const result = await intersect(departmentACourses, departmentBCourses);
select `projects_name` from `department_a_projects`
intersect
select `projects_name` from `department_b_projects`
import { depA, depB } from './schema'
const result = await db
.select({ courseName: depA.courseName })
.from(depA)
.intersect(db.select({ courseName: depB.courseName }).from(depB));
select `projects_name` from `department_a_projects`
intersect
select `projects_name` from `department_b_projects`
import { int, mysqlTable, varchar } from "drizzle-orm/singlestore-core";
const depA = mysqlTable('department_a_courses', {
studentId: int('student_id'),
courseName: varchar('course_name', { length: 256 }).notNull(),
});
const depB = pgTable('department_b_courses', {
studentId: int('student_id'),
courseName: varchar('course_name', { length: 256 }).notNull(),
});
交集所有
只合并两个查询块结果中共同的行,包含重复项。
假设您有两个表,其中包含客户订单数据,您想找出普通客户和 VIP 客户都订购过的产品。在这种情况下,您希望跟踪每种产品的数量,即使不同客户多次订购了同一产品。
在这种情况下,您想找出普通客户和 VIP 客户都订购过的产品,但您希望保留数量信息,即使不同客户多次订购了同一产品。
import-pattern
builder-pattern
schema.ts
import { intersectAll } from 'drizzle-orm/pg-core'
import { regularCustomerOrders, vipCustomerOrders } from './schema'
const regularOrders = db.select({
productId: regularCustomerOrders.productId,
quantityOrdered: regularCustomerOrders.quantityOrdered }
).from(regularCustomerOrders);
const vipOrders = db.select({
productId: vipCustomerOrders.productId,
quantityOrdered: vipCustomerOrders.quantityOrdered }
).from(vipCustomerOrders);
const result = await intersectAll(regularOrders, vipOrders);
select "product_id", "quantity_ordered" from "regular_customer_orders"
intersect all
select "product_id", "quantity_ordered" from "vip_customer_orders"
import { regularCustomerOrders, vipCustomerOrders } from './schema'
const result = await db
.select({
productId: regularCustomerOrders.productId,
quantityOrdered: regularCustomerOrders.quantityOrdered,
})
.from(regularCustomerOrders)
.intersectAll(
db
.select({
productId: vipCustomerOrders.productId,
quantityOrdered: vipCustomerOrders.quantityOrdered,
})
.from(vipCustomerOrders)
);
select "product_id", "quantity_ordered" from "regular_customer_orders"
intersect all
select "product_id", "quantity_ordered" from "vip_customer_orders"
import { integer, pgTable } from "drizzle-orm/pg-core";
const regularCustomerOrders = pgTable('regular_customer_orders', {
customerId: integer('customer_id').primaryKey(),
productId: integer('product_id').notNull(),
quantityOrdered: integer('quantity_ordered').notNull(),
});
const vipCustomerOrders = pgTable('vip_customer_orders', {
customerId: integer('customer_id').primaryKey(),
productId: integer('product_id').notNull(),
quantityOrdered: integer('quantity_ordered').notNull(),
});
import-pattern
builder-pattern
schema.ts
import { intersectAll } from 'drizzle-orm/mysql-core'
import { regularCustomerOrders, vipCustomerOrders } from './schema'
const regularOrders = db.select({
productId: regularCustomerOrders.productId,
quantityOrdered: regularCustomerOrders.quantityOrdered }
).from(regularCustomerOrders);
const vipOrders = db.select({
productId: vipCustomerOrders.productId,
quantityOrdered: vipCustomerOrders.quantityOrdered }
).from(vipCustomerOrders);
const result = await intersectAll(regularOrders, vipOrders);
select `product_id`, `quantity_ordered` from `regular_customer_orders`
intersect all
select `product_id`, `quantity_ordered` from `vip_customer_orders`
import { regularCustomerOrders, vipCustomerOrders } from './schema'
const result = await db
.select({
productId: regularCustomerOrders.productId,
quantityOrdered: regularCustomerOrders.quantityOrdered,
})
.from(regularCustomerOrders)
.intersectAll(
db
.select({
productId: vipCustomerOrders.productId,
quantityOrdered: vipCustomerOrders.quantityOrdered,
})
.from(vipCustomerOrders)
);
select `product_id`, `quantity_ordered` from `regular_customer_orders`
intersect all
select `product_id`, `quantity_ordered` from `vip_customer_orders`
import { int, mysqlTable } from "drizzle-orm/mysql-core";
const regularCustomerOrders = mysqlTable('regular_customer_orders', {
customerId: int('customer_id').primaryKey(),
productId: int('product_id').notNull(),
quantityOrdered: int('quantity_ordered').notNull(),
});
const vipCustomerOrders = mysqlTable('vip_customer_orders', {
customerId: int('customer_id').primaryKey(),
productId: int('product_id').notNull(),
quantityOrdered: int('quantity_ordered').notNull(),
});
差集
对于两个查询块 A 和 B,返回 A 中存在但 B 中不存在的所有结果,并省略所有重复项。
假设您有两个表,存储了员工的项目分配信息。您想找出某个部门独有的项目,不与其他部门共享,并排除重复项。
在这种情况下,您想识别某个部门独有且不与其他部门共享的项目。您不想多次计数同一个项目,即使同一部门的多个员工被分配到该项目。
import-pattern
builder-pattern
schema.ts
import { except } from 'drizzle-orm/pg-core'
import { depA, depB } from './schema'
const departmentACourses = db.select({ courseName: depA.projectsName }).from(depA);
const departmentBCourses = db.select({ courseName: depB.projectsName }).from(depB);
const result = await except(departmentACourses, departmentBCourses);
select "projects_name" from "department_a_projects"
except
select "projects_name" from "department_b_projects"
import { depA, depB } from './schema'
const result = await db
.select({ courseName: depA.projectsName })
.from(depA)
.except(db.select({ courseName: depB.projectsName }).from(depB));
select "projects_name" from "department_a_projects"
except
select "projects_name" from "department_b_projects"
import { integer, pgTable, varchar } from "drizzle-orm/pg-core";
const depA = pgTable('department_a_projects', {
employeeId: integer('employee_id'),
projectsName: varchar('projects_name').notNull(),
});
const depB = pgTable('department_b_projects', {
employeeId: integer('employee_id'),
projectsName: varchar('projects_name').notNull(),
});
import-pattern
builder-pattern
schema.ts
import { except } from 'drizzle-orm/mysql-core'
import { depA, depB } from './schema'
const departmentACourses = db.select({ courseName: depA.projectsName }).from(depA);
const departmentBCourses = db.select({ courseName: depB.projectsName }).from(depB);
const result = await except(departmentACourses, departmentBCourses);
select `projects_name` from `department_a_projects`
except
select `projects_name` from `department_b_projects`
import { depA, depB } from './schema'
const result = await db
.select({ courseName: depA.projectsName })
.from(depA)
.except(db.select({ courseName: depB.projectsName }).from(depB));
select `projects_name` from `department_a_projects`
except
select `projects_name` from `department_b_projects`
import { int, mysqlTable, varchar } from "drizzle-orm/mysql-core";
const depA = mysqlTable('department_a_projects', {
employeeId: int('employee_id'),
projectsName: varchar('projects_name', { length: 256 }).notNull(),
});
const depB = mysqlTable('department_b_projects', {
employeeId: int('employee_id'),
projectsName: varchar('projects_name', { length: 256 }).notNull(),
});
import-pattern
builder-pattern
schema.ts
import { except } from 'drizzle-orm/sqlite-core'
import { depA, depB } from './schema'
const departmentACourses = db.select({ courseName: depA.projectsName }).from(depA);
const departmentBCourses = db.select({ courseName: depB.projectsName }).from(depB);
const result = await except(departmentACourses, departmentBCourses);
select "projects_name" from "department_a_projects"
except
select "projects_name" from "department_b_projects"
import { depA, depB } from './schema'
const result = await db
.select({ courseName: depA.projectsName })
.from(depA)
.except(db.select({ courseName: depB.projectsName }).from(depB));
select "projects_name" from "department_a_projects"
except
select "projects_name" from "department_b_projects"
import { int, sqliteTable, text } from "drizzle-orm/sqlite-core";
const depA = sqliteTable('department_a_projects', {
employeeId: int('employee_id'),
projectsName: text('projects_name').notNull(),
});
const depB = sqliteTable('department_b_projects', {
employeeId: int('employee_id'),
projectsName: text('projects_name').notNull(),
});
import-pattern
builder-pattern
schema.ts
import { except } from 'drizzle-orm/singlestore-core'
import { depA, depB } from './schema'
const departmentACourses = db.select({ courseName: depA.projectsName }).from(depA);
const departmentBCourses = db.select({ courseName: depB.projectsName }).from(depB);
const result = await except(departmentACourses, departmentBCourses);
select `projects_name` from `department_a_projects`
except
select `projects_name` from `department_b_projects`
import { depA, depB } from './schema'
const result = await db
.select({ courseName: depA.projectsName })
.from(depA)
.except(db.select({ courseName: depB.projectsName }).from(depB));
select `projects_name` from `department_a_projects`
except
select `projects_name` from `department_b_projects`
import { int, mysqlTable, varchar } from "drizzle-orm/singlestore-core";
const depA = mysqlTable('department_a_projects', {
employeeId: int('employee_id'),
projectsName: varchar('projects_name', { length: 256 }).notNull(),
});
const depB = mysqlTable('department_b_projects', {
employeeId: int('employee_id'),
projectsName: varchar('projects_name', { length: 256 }).notNull(),
});
差集所有
对于两个查询块 A 和 B,返回 A 中存在但 B 中不存在的所有结果,包含重复项。
假设您有两个表,其中包含客户订单数据,您想找出普通客户(不含 VIP 客户)独家订购的产品。在这种情况下,您希望跟踪每种产品的数量,即使不同普通客户多次订购了同一产品。
在这种情况下,您想找出普通客户独家订购且 VIP 客户未订购的产品。您希望保留数量信息,即使同一产品被不同普通客户多次订购。
import-pattern
builder-pattern
schema.ts
import { exceptAll } from 'drizzle-orm/pg-core'
import { regularCustomerOrders, vipCustomerOrders } from './schema'
const regularOrders = db.select({
productId: regularCustomerOrders.productId,
quantityOrdered: regularCustomerOrders.quantityOrdered }
).from(regularCustomerOrders);
const vipOrders = db.select({
productId: vipCustomerOrders.productId,
quantityOrdered: vipCustomerOrders.quantityOrdered }
).from(vipCustomerOrders);
const result = await exceptAll(regularOrders, vipOrders);
select "product_id", "quantity_ordered" from "regular_customer_orders"
except all
select "product_id", "quantity_ordered" from "vip_customer_orders"
import { regularCustomerOrders, vipCustomerOrders } from './schema'
const result = await db
.select({
productId: regularCustomerOrders.productId,
quantityOrdered: regularCustomerOrders.quantityOrdered,
})
.from(regularCustomerOrders)
.exceptAll(
db
.select({
productId: vipCustomerOrders.productId,
quantityOrdered: vipCustomerOrders.quantityOrdered,
})
.from(vipCustomerOrders)
);
select "product_id", "quantity_ordered" from "regular_customer_orders"
except all
select "product_id", "quantity_ordered" from "vip_customer_orders"
import { integer, pgTable } from "drizzle-orm/pg-core";
const regularCustomerOrders = pgTable('regular_customer_orders', {
customerId: integer('customer_id').primaryKey(),
productId: integer('product_id').notNull(),
quantityOrdered: integer('quantity_ordered').notNull(),
});
const vipCustomerOrders = pgTable('vip_customer_orders', {
customerId: integer('customer_id').primaryKey(),
productId: integer('product_id').notNull(),
quantityOrdered: integer('quantity_ordered').notNull(),
});
import-pattern
builder-pattern
schema.ts
import { exceptAll } from 'drizzle-orm/mysql-core'
import { regularCustomerOrders, vipCustomerOrders } from './schema'
const regularOrders = db.select({
productId: regularCustomerOrders.productId,
quantityOrdered: regularCustomerOrders.quantityOrdered }
).from(regularCustomerOrders);
const vipOrders = db.select({
productId: vipCustomerOrders.productId,
quantityOrdered: vipCustomerOrders.quantityOrdered }
).from(vipCustomerOrders);
const result = await exceptAll(regularOrders, vipOrders);
select `product_id`, `quantity_ordered` from `regular_customer_orders`
except all
select `product_id`, `quantity_ordered` from `vip_customer_orders`
import { regularCustomerOrders, vipCustomerOrders } from './schema'
const result = await db
.select({
productId: regularCustomerOrders.productId,
quantityOrdered: regularCustomerOrders.quantityOrdered,
})
.from(regularCustomerOrders)
.exceptAll(
db
.select({
productId: vipCustomerOrders.productId,
quantityOrdered: vipCustomerOrders.quantityOrdered,
})
.from(vipCustomerOrders)
);
select `product_id`, `quantity_ordered` from `regular_customer_orders`
except all
select `product_id`, `quantity_ordered` from `vip_customer_orders`
const regularCustomerOrders = mysqlTable('regular_customer_orders', {
customerId: int('customer_id').primaryKey(),
productId: int('product_id').notNull(),
quantityOrdered: int('quantity_ordered').notNull(),
});
const vipCustomerOrders = mysqlTable('vip_customer_orders', {
customerId: int('customer_id').primaryKey(),
productId: int('product_id').notNull(),
quantityOrdered: int('quantity_ordered').notNull(),
});