์งํฉ ์ฐ์ฐ
SQL ์งํฉ ์ฐ์ฐ์ ์ฌ๋ฌ ์ฟผ๋ฆฌ ๋ธ๋ก์ ๊ฒฐ๊ณผ๋ฅผ ํ๋์ ๊ฒฐ๊ณผ๋ก ๊ฒฐํฉํฉ๋๋ค.
SQL ํ์ค์ ๋ค์๊ณผ ๊ฐ์ ์ธ ๊ฐ์ง ์งํฉ ์ฐ์ฐ์ ์ ์ํฉ๋๋ค: UNION, INTERSECT, EXCEPT, UNION ALL, INTERSECT ALL, EXCEPT ALL.
Union
๋ ์ฟผ๋ฆฌ ๋ธ๋ก์ ๋ชจ๋ ๊ฒฐ๊ณผ๋ฅผ ํ๋์ ๊ฒฐ๊ณผ๋ก ๊ฒฐํฉํ๋ฉฐ, ์ค๋ณต์ ์ ๊ฑฐํฉ๋๋ค.
๊ณ ๊ฐ ํ
์ด๋ธ๊ณผ ์ฌ์ฉ์ ํ
์ด๋ธ์์ ์ค๋ณต ์์ด ๋ชจ๋ ์ด๋ฆ์ ๊ฐ์ ธ์ต๋๋ค.
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()
});
Union All
๋ ์ฟผ๋ฆฌ ๋ธ๋ก์ ๋ชจ๋ ๊ฒฐ๊ณผ๋ฅผ ํ๋์ ๊ฒฐ๊ณผ๋ก ๊ฒฐํฉํ๋ฉฐ, ์ค๋ณต์ ์ ์งํฉ๋๋ค.
์จ๋ผ์ธ ํ๋งค๋ฅผ ๋ํ๋ด๋ ํ
์ด๋ธ๊ณผ ๋งค์ฅ ๋ด ํ๋งค๋ฅผ ๋ํ๋ด๋ ํ
์ด๋ธ์ด ์๋ ์๋๋ฆฌ์ค๋ฅผ ์๊ฐํด ๋ณด๊ฒ ์ต๋๋ค.
์ด ๊ฒฝ์ฐ ๋ ํ
์ด๋ธ์ ๋ฐ์ดํฐ๋ฅผ ํ๋์ ๊ฒฐ๊ณผ ์งํฉ์ผ๋ก ๊ฒฐํฉํ๋ ค๊ณ ํฉ๋๋ค.
์ค๋ณต๋ ๊ฑฐ๋๊ฐ ์์ ์ ์์ผ๋ฏ๋ก, ์ค๋ณต์ ์ ๊ฑฐํ์ง ์๊ณ ๋ชจ๋ ๋ ์ฝ๋๋ฅผ ์ ์งํ๋ ค๊ณ ํฉ๋๋ค.
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' }),
});
IMPORTANT
ORDER BY์ ํจ๊ป ์ฌ์ฉ๋๋ UNION ALL์ ๋์์ด 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' }),
});
Intersect
๋ ์ฟผ๋ฆฌ ๋ธ๋ก์ ๊ฒฐ๊ณผ์ ๊ณตํต์ ์ผ๋ก ์๋ ํ๋ง ๊ฒฐํฉํ๋ฉฐ, ์ค๋ณต์ ์ ๊ฑฐํฉ๋๋ค.
ํ์๋ค์ ์๊ฐ ์ ์ฒญ ์ ๋ณด๋ฅผ ์ ์ฅํ๋ ๋ ๊ฐ์ ํ
์ด๋ธ์ด ์๋ค๊ณ ๊ฐ์ ํฉ๋๋ค.
๋ ๊ฐ์ ๋ค๋ฅธ ํ๊ณผ ๊ฐ์ ๊ณตํต์ผ๋ก ์๋ ๊ณผ๋ชฉ์ ์ฐพ๊ณ ์ถ์ง๋ง,
๊ณ ์ ํ ๊ณผ๋ชฉ ์ด๋ฆ๋ง ์ํ๋ฉฐ, ๋์ผํ ํ์์ด ๋์ผํ ๊ณผ๋ชฉ์ ์ฌ๋ฌ ๋ฒ ์๊ฐํ ๊ฒ์๋ ๊ด์ฌ์ด ์์ต๋๋ค.
์ด ์๋๋ฆฌ์ค์์๋ ๋ ํ๊ณผ ๊ฐ์ ๊ณตํต์ผ๋ก ์๋ ๊ณผ๋ชฉ์ ์ฐพ๋, ๊ฐ์ ํ๊ณผ์ ์ฌ๋ฌ ํ์์ด ์๊ฐํ๋๋ผ๋
๋์ผํ ๊ณผ๋ชฉ์ ์ฌ๋ฌ ๋ฒ ์ธ์ง ์์ผ๋ ค๊ณ ํฉ๋๋ค.
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(),
});
Intersect All
๋ ์ฟผ๋ฆฌ ๋ธ๋ก์ ๊ฒฐ๊ณผ์ ๊ณตํต์ ์ผ๋ก ์๋ ํ๋ง ๊ฒฐํฉํ๋ฉฐ, ์ค๋ณต์ ์ ์งํฉ๋๋ค.
๊ณ ๊ฐ ์ฃผ๋ฌธ ๋ฐ์ดํฐ๊ฐ ํฌํจ๋ ๋ ๊ฐ์ ํ
์ด๋ธ์ด ์๊ณ , ์ผ๋ฐ ๊ณ ๊ฐ๊ณผ 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(),
});
SingleStore์์ ์ง์๋์ง ์์ต๋๋ค
Except
๋ ์ฟผ๋ฆฌ ๋ธ๋ก 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(),
});
Except All
๋ ์ฟผ๋ฆฌ ๋ธ๋ก 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(),
});
SingleStore์์ ์ง์๋์ง ์์ต๋๋ค