UNION ALL with ORDER BY behavior inconsistent with MySQL: SingleStore parses UNION ALL followed by ORDER BY commands differently from MySQL. In SingleStore, the following query is valid. In MySQL, it is invalid.
集合操作
SQL 集合操作将多个查询块的结果结合成单个结果。
SQL 标准定义了以下三种集合操作:UNION、INTERSECT、EXCEPT、UNION ALL、INTERSECT ALL、EXCEPT ALL。
并集(Union)
将两个查询块的所有结果组合成单个结果,省略任何重复项。
获取 customers 和 users 表中的所有名称,不带重复。
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 $1import { 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 $1import { 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 { 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 { 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 { 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 { union } from 'drizzle-orm/mssql-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 @limitimport { 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 @limitimport { int, mssqlTable, ntext, nvarchar } from "drizzle-orm/mssql-core";
const users = mssqlTable('sellers', {
id: int().primaryKey(),
name: nvarchar({ length: 256 }).notNull(),
address: ntext(),
});
const customers = mssqlTable('customers', {
id: int().primaryKey(),
name: nvarchar({ length: 256 }).notNull(),
city: ntext(),
email: nvarchar({ length: 256 }).notNull()
});import { union } from 'drizzle-orm/cockroach-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 $1import { 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 $1import { int4, cockroachTable, text, varchar } from "drizzle-orm/cockroach-core";
const users = cockroachTable('sellers', {
id: int4().primaryKey(),
name: varchar({ length: 256 }).notNull(),
address: text(),
});
const customers = cockroachTable('customers', {
id: int4().primaryKey(),
name: varchar({ length: 256 }).notNull(),
city: text(),
email: varchar({ length: 256 }).notNull()
});并集所有(Union All)
将两个查询块的所有结果组合成单个结果,包括重复项。
考虑一个场景,你有两个表,一个表示在线销售, 另一个表示门店销售。在这种情况下,你想把两个表的数据结合成一个结果集。 由于可能会有重复的交易, 你希望保留所有记录,而不消除重复项。
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 { 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 { 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' }),
});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 { unionAll } from 'drizzle-orm/mssql-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, mssqlTable, timestamp } from "drizzle-orm/mssql-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 { unionAll } from 'drizzle-orm/cockroach-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 { int4, cockroachTable, text, timestamp, varchar } from "drizzle-orm/cockroach-core";
const onlineSales = cockroachTable('online_sales', {
transactionId: int4('transaction_id').primaryKey(),
productId: int4('product_id').unique(),
quantitySold: int4('quantity_sold'),
saleDate: timestamp('sale_date', { mode: 'date' }),
});
const inStoreSales = cockroachTable('in_store_sales', {
transactionId: int4('transaction_id').primaryKey(),
productId: int4('product_id').unique(),
quantitySold: int4('quantity_sold'),
saleDate: timestamp('sale_date', { mode: 'date' }),
});交集(Intersect)
仅结合两个查询块的结果中共通的行,省略任何重复项。
假设你有两个表存储有关学生课程注册的信息。 你想找到两个不同系间共同的课程, 但你希望课程名是唯一的, 并且不想计算同一个学生在同一课程下的多个注册。
在此场景中,你希望找到两个系间的共同课程, 但不希望同一课程即使被同一系的多个学生注册也计数多次。
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 { 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 = mysqlTable('department_b_courses', {
studentId: int('student_id'),
courseName: varchar('course_name', { length: 256 }).notNull(),
});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 { 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, singlestoreTable, varchar } from "drizzle-orm/singlestore-core";
const depA = singlestoreTable('department_a_courses', {
studentId: int('student_id'),
courseName: varchar('course_name', { length: 256 }).notNull(),
});
const depB = singlestoreTable('department_b_courses', {
studentId: int('student_id'),
courseName: varchar('course_name', { length: 256 }).notNull(),
});import { intersect } from 'drizzle-orm/mssql-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, mssqlTable, varchar } from "drizzle-orm/mssqlTable-core";
const depA = mssqlTable('department_a_courses', {
studentId: int('student_id'),
courseName: varchar('course_name', { length: 256 }).notNull(),
});
const depB = mssqlTable('department_b_courses', {
studentId: int('student_id'),
courseName: varchar('course_name', { length: 256 }).notNull(),
});import { intersect } from 'drizzle-orm/cockroach-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 { int4, cockroachTable, varchar } from "drizzle-orm/cockroach-core";
const depA = cockroachTable('department_a_courses', {
studentId: int4('student_id'),
courseName: varchar('course_name').notNull(),
});
const depB = cockroachTable('department_b_courses', {
studentId: int4('student_id'),
courseName: varchar('course_name').notNull(),
});交集所有(Intersect All)
仅结合两个查询块的结果中共通的行,包括重复项。
让我们考虑一个场景,你有两个表包含有关客户订单的数据, 你想识别普通客户和 VIP 客户都订单的产品。 在这种情况下,你想更好地跟踪每种产品的数量, 即使不同客户多次下单。
在此场景中,你希望找到普通客户和 VIP 客户都下单的产品, 但你希望保留数量信息, 即使同一产品被不同的客户多次下单。
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 { 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(),
});Not supported by SQLite
Not supported by SingleStore
Not supported by MSSQL
import { intersectAll } from 'drizzle-orm/cockroach-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 { int4, cockroachTable } from "drizzle-orm/cockroach-core";
const regularCustomerOrders = cockroachTable('regular_customer_orders', {
customerId: int4('customer_id').primaryKey(),
productId: int4('product_id').notNull(),
quantityOrdered: int4('quantity_ordered').notNull(),
});
const vipCustomerOrders = cockroachTable('vip_customer_orders', {
customerId: int4('customer_id').primaryKey(),
productId: int4('product_id').notNull(),
quantityOrdered: int4('quantity_ordered').notNull(),
});差集(Except)
对于两个查询块 A 和 B,返回不在 B 中的 A 的所有结果,省略任何重复项。
假设你有两个表存储有关员工项目分配的信息。 你想找到唯一属于一个部门并且不与另一个部门共享的项目, 排除重复项。
在此场景中,你想识别唯一属于一个部门而不与另一个部门共享的项目。 你不希望同一项目计数多次, 即使同一部门的多个员工都被分配给该项目。
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 { 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 { 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 { 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, singlestoreTable, varchar } from "drizzle-orm/singlestore-core";
const depA = singlestoreTable('department_a_projects', {
employeeId: int('employee_id'),
projectsName: varchar('projects_name', { length: 256 }).notNull(),
});
const depB = singlestoreTable('department_b_projects', {
employeeId: int('employee_id'),
projectsName: varchar('projects_name', { length: 256 }).notNull(),
});import { except } from 'drizzle-orm/mssql-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, mssqlTable, nvarchar } from "drizzle-orm/mssql-core";
const depA = mssqlTable('department_a_projects', {
employeeId: int('employee_id'),
projectsName: nvarchar('projects_name', { length: 256 }).notNull(),
});
const depB = mssqlTable('department_b_projects', {
employeeId: int('employee_id'),
projectsName: nvarchar('projects_name', { length: 256 }).notNull(),
});import { except } from 'drizzle-orm/cockroach-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 { int4, cockroachTable, varchar } from "drizzle-orm/cockroach-core";
const depA = cockroachTable('department_a_projects', {
employeeId: int4('employee_id'),
projectsName: varchar('projects_name').notNull(),
});
const depB = cockroachTable('department_b_projects', {
employeeId: int4('employee_id'),
projectsName: varchar('projects_name').notNull(),
});差集所有(Except All)
对于两个查询块 A 和 B,返回不在 B 中的 A 的所有结果,包括重复项。
让我们考虑一个场景,你有两个表包含有关客户订单的数据, 你想识别仅普通客户(没有 VIP 客户)下单的产品。 在这种情况下,你希望保留每种产品的数量信息,即使不同的普通客户多次下单。
在此场景中,你希望找到仅由普通客户下单的产品,并且不由 VIP 客户下单。 你希望保留数量信息,即使同一产品被不同的普通客户多次下单。
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 { 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(),
});Not supported by SQLite
Not supported by SingleStore
Not supported by MSSQL
import { exceptAll } from 'drizzle-orm/cockroach-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 { int4, cockroachTable } from "drizzle-orm/cockroach-core";
const regularCustomerOrders = cockroachTable('regular_customer_orders', {
customerId: int4('customer_id').primaryKey(),
productId: int4('product_id').notNull(),
quantityOrdered: int4('quantity_ordered').notNull(),
});
const vipCustomerOrders = cockroachTable('vip_customer_orders', {
customerId: int4('customer_id').primaryKey(),
productId: int4('product_id').notNull(),
quantityOrdered: int4('quantity_ordered').notNull(),
});