ORDER BY가 포함된 UNION ALL의 동작이 MySQL과 일관되지 않음: SingleStore는 UNION ALL 다음에 오는 ORDER BY 명령을 MySQL과 다르게 해석합니다. SingleStore에서는 다음 쿼리가 유효하지만 MySQL에서는 유효하지 않습니다.
집합 연산
집합 연산
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(),
});SQLite에서 지원하지 않음
SingleStore에서 지원하지 않음
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(),
});SQLite에서 지원하지 않음
SingleStore에서 지원하지 않음
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(),
});