select.d.ts 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803
  1. import type { CacheConfig, WithCacheConfig } from "../../cache/core/types.js";
  2. import { entityKind } from "../../entity.js";
  3. import type { MySqlColumn } from "../columns/index.js";
  4. import type { MySqlDialect } from "../dialect.js";
  5. import type { MySqlSession, PreparedQueryHKTBase } from "../session.js";
  6. import type { SubqueryWithSelection } from "../subquery.js";
  7. import { MySqlTable } from "../table.js";
  8. import { TypedQueryBuilder } from "../../query-builders/query-builder.js";
  9. import type { BuildSubquerySelection, GetSelectTableName, GetSelectTableSelection, JoinNullability, SelectMode, SelectResult } from "../../query-builders/select.types.js";
  10. import { QueryPromise } from "../../query-promise.js";
  11. import type { ColumnsSelection, Placeholder, Query } from "../../sql/sql.js";
  12. import { SQL } from "../../sql/sql.js";
  13. import { Subquery } from "../../subquery.js";
  14. import type { ValueOrArray } from "../../utils.js";
  15. import type { IndexBuilder } from "../indexes.js";
  16. import { MySqlViewBase } from "../view-base.js";
  17. import type { CreateMySqlSelectFromBuilderMode, GetMySqlSetOperators, LockConfig, LockStrength, MySqlCreateSetOperatorFn, MySqlCrossJoinFn, MySqlJoinFn, MySqlSelectConfig, MySqlSelectDynamic, MySqlSelectHKT, MySqlSelectHKTBase, MySqlSelectPrepare, MySqlSelectWithout, MySqlSetOperatorExcludedMethods, MySqlSetOperatorWithResult, SelectedFields, SetOperatorRightSelect } from "./select.types.js";
  18. export type IndexForHint = IndexBuilder | string;
  19. export type IndexConfig = {
  20. useIndex?: IndexForHint | IndexForHint[];
  21. forceIndex?: IndexForHint | IndexForHint[];
  22. ignoreIndex?: IndexForHint | IndexForHint[];
  23. };
  24. export declare class MySqlSelectBuilder<TSelection extends SelectedFields | undefined, TPreparedQueryHKT extends PreparedQueryHKTBase, TBuilderMode extends 'db' | 'qb' = 'db'> {
  25. static readonly [entityKind]: string;
  26. private fields;
  27. private session;
  28. private dialect;
  29. private withList;
  30. private distinct;
  31. constructor(config: {
  32. fields: TSelection;
  33. session: MySqlSession | undefined;
  34. dialect: MySqlDialect;
  35. withList?: Subquery[];
  36. distinct?: boolean;
  37. });
  38. from<TFrom extends MySqlTable | Subquery | MySqlViewBase | SQL>(source: TFrom, onIndex?: TFrom extends MySqlTable ? IndexConfig : 'Index hint configuration is allowed only for MySqlTable and not for subqueries or views'): CreateMySqlSelectFromBuilderMode<TBuilderMode, GetSelectTableName<TFrom>, TSelection extends undefined ? GetSelectTableSelection<TFrom> : TSelection, TSelection extends undefined ? 'single' : 'partial', TPreparedQueryHKT>;
  39. }
  40. export declare abstract class MySqlSelectQueryBuilderBase<THKT extends MySqlSelectHKTBase, TTableName extends string | undefined, TSelection extends ColumnsSelection, TSelectMode extends SelectMode, TPreparedQueryHKT extends PreparedQueryHKTBase, TNullabilityMap extends Record<string, JoinNullability> = TTableName extends string ? Record<TTableName, 'not-null'> : {}, TDynamic extends boolean = false, TExcludedMethods extends string = never, TResult extends any[] = SelectResult<TSelection, TSelectMode, TNullabilityMap>[], TSelectedFields extends ColumnsSelection = BuildSubquerySelection<TSelection, TNullabilityMap>> extends TypedQueryBuilder<TSelectedFields, TResult> {
  41. static readonly [entityKind]: string;
  42. readonly _: {
  43. readonly hkt: THKT;
  44. readonly tableName: TTableName;
  45. readonly selection: TSelection;
  46. readonly selectMode: TSelectMode;
  47. readonly preparedQueryHKT: TPreparedQueryHKT;
  48. readonly nullabilityMap: TNullabilityMap;
  49. readonly dynamic: TDynamic;
  50. readonly excludedMethods: TExcludedMethods;
  51. readonly result: TResult;
  52. readonly selectedFields: TSelectedFields;
  53. readonly config: MySqlSelectConfig;
  54. };
  55. protected config: MySqlSelectConfig;
  56. protected joinsNotNullableMap: Record<string, boolean>;
  57. private tableName;
  58. private isPartialSelect;
  59. protected dialect: MySqlDialect;
  60. protected cacheConfig?: WithCacheConfig;
  61. protected usedTables: Set<string>;
  62. constructor({ table, fields, isPartialSelect, session, dialect, withList, distinct, useIndex, forceIndex, ignoreIndex }: {
  63. table: MySqlSelectConfig['table'];
  64. fields: MySqlSelectConfig['fields'];
  65. isPartialSelect: boolean;
  66. session: MySqlSession | undefined;
  67. dialect: MySqlDialect;
  68. withList: Subquery[];
  69. distinct: boolean | undefined;
  70. useIndex?: string[];
  71. forceIndex?: string[];
  72. ignoreIndex?: string[];
  73. });
  74. private createJoin;
  75. /**
  76. * Executes a `left join` operation by adding another table to the current query.
  77. *
  78. * Calling this method associates each row of the table with the corresponding row from the joined table, if a match is found. If no matching row exists, it sets all columns of the joined table to null.
  79. *
  80. * See docs: {@link https://orm.drizzle.team/docs/joins#left-join}
  81. *
  82. * @param table the table to join.
  83. * @param on the `on` clause.
  84. * @param onIndex index hint.
  85. *
  86. * @example
  87. *
  88. * ```ts
  89. * // Select all users and their pets
  90. * const usersWithPets: { user: User; pets: Pet | null; }[] = await db.select()
  91. * .from(users)
  92. * .leftJoin(pets, eq(users.id, pets.ownerId))
  93. *
  94. * // Select userId and petId
  95. * const usersIdsAndPetIds: { userId: number; petId: number | null; }[] = await db.select({
  96. * userId: users.id,
  97. * petId: pets.id,
  98. * })
  99. * .from(users)
  100. * .leftJoin(pets, eq(users.id, pets.ownerId))
  101. *
  102. * // Select userId and petId with use index hint
  103. * const usersIdsAndPetIds: { userId: number; petId: number | null; }[] = await db.select({
  104. * userId: users.id,
  105. * petId: pets.id,
  106. * })
  107. * .from(users)
  108. * .leftJoin(pets, eq(users.id, pets.ownerId), {
  109. * useIndex: ['pets_owner_id_index']
  110. * })
  111. * ```
  112. */
  113. leftJoin: MySqlJoinFn<this, TDynamic, "left", false>;
  114. /**
  115. * Executes a `left join lateral` operation by adding subquery to the current query.
  116. *
  117. * A `lateral` join allows the right-hand expression to refer to columns from the left-hand side.
  118. *
  119. * Calling this method associates each row of the table with the corresponding row from the joined table, if a match is found. If no matching row exists, it sets all columns of the joined table to null.
  120. *
  121. * See docs: {@link https://orm.drizzle.team/docs/joins#left-join-lateral}
  122. *
  123. * @param table the subquery to join.
  124. * @param on the `on` clause.
  125. */
  126. leftJoinLateral: MySqlJoinFn<this, TDynamic, "left", true>;
  127. /**
  128. * Executes a `right join` operation by adding another table to the current query.
  129. *
  130. * Calling this method associates each row of the joined table with the corresponding row from the main table, if a match is found. If no matching row exists, it sets all columns of the main table to null.
  131. *
  132. * See docs: {@link https://orm.drizzle.team/docs/joins#right-join}
  133. *
  134. * @param table the table to join.
  135. * @param on the `on` clause.
  136. * @param onIndex index hint.
  137. *
  138. * @example
  139. *
  140. * ```ts
  141. * // Select all users and their pets
  142. * const usersWithPets: { user: User | null; pets: Pet; }[] = await db.select()
  143. * .from(users)
  144. * .rightJoin(pets, eq(users.id, pets.ownerId))
  145. *
  146. * // Select userId and petId
  147. * const usersIdsAndPetIds: { userId: number | null; petId: number; }[] = await db.select({
  148. * userId: users.id,
  149. * petId: pets.id,
  150. * })
  151. * .from(users)
  152. * .rightJoin(pets, eq(users.id, pets.ownerId))
  153. *
  154. * // Select userId and petId with use index hint
  155. * const usersIdsAndPetIds: { userId: number; petId: number | null; }[] = await db.select({
  156. * userId: users.id,
  157. * petId: pets.id,
  158. * })
  159. * .from(users)
  160. * .leftJoin(pets, eq(users.id, pets.ownerId), {
  161. * useIndex: ['pets_owner_id_index']
  162. * })
  163. * ```
  164. */
  165. rightJoin: MySqlJoinFn<this, TDynamic, "right", false>;
  166. /**
  167. * Executes an `inner join` operation, creating a new table by combining rows from two tables that have matching values.
  168. *
  169. * Calling this method retrieves rows that have corresponding entries in both joined tables. Rows without matching entries in either table are excluded, resulting in a table that includes only matching pairs.
  170. *
  171. * See docs: {@link https://orm.drizzle.team/docs/joins#inner-join}
  172. *
  173. * @param table the table to join.
  174. * @param on the `on` clause.
  175. * @param onIndex index hint.
  176. *
  177. * @example
  178. *
  179. * ```ts
  180. * // Select all users and their pets
  181. * const usersWithPets: { user: User; pets: Pet; }[] = await db.select()
  182. * .from(users)
  183. * .innerJoin(pets, eq(users.id, pets.ownerId))
  184. *
  185. * // Select userId and petId
  186. * const usersIdsAndPetIds: { userId: number; petId: number; }[] = await db.select({
  187. * userId: users.id,
  188. * petId: pets.id,
  189. * })
  190. * .from(users)
  191. * .innerJoin(pets, eq(users.id, pets.ownerId))
  192. *
  193. * // Select userId and petId with use index hint
  194. * const usersIdsAndPetIds: { userId: number; petId: number | null; }[] = await db.select({
  195. * userId: users.id,
  196. * petId: pets.id,
  197. * })
  198. * .from(users)
  199. * .leftJoin(pets, eq(users.id, pets.ownerId), {
  200. * useIndex: ['pets_owner_id_index']
  201. * })
  202. * ```
  203. */
  204. innerJoin: MySqlJoinFn<this, TDynamic, "inner", false>;
  205. /**
  206. * Executes an `inner join lateral` operation, creating a new table by combining rows from two queries that have matching values.
  207. *
  208. * A `lateral` join allows the right-hand expression to refer to columns from the left-hand side.
  209. *
  210. * Calling this method retrieves rows that have corresponding entries in both joined tables. Rows without matching entries in either table are excluded, resulting in a table that includes only matching pairs.
  211. *
  212. * See docs: {@link https://orm.drizzle.team/docs/joins#inner-join-lateral}
  213. *
  214. * @param table the subquery to join.
  215. * @param on the `on` clause.
  216. */
  217. innerJoinLateral: MySqlJoinFn<this, TDynamic, "inner", true>;
  218. /**
  219. * Executes a `cross join` operation by combining rows from two tables into a new table.
  220. *
  221. * Calling this method retrieves all rows from both main and joined tables, merging all rows from each table.
  222. *
  223. * See docs: {@link https://orm.drizzle.team/docs/joins#cross-join}
  224. *
  225. * @param table the table to join.
  226. * @param onIndex index hint.
  227. *
  228. * @example
  229. *
  230. * ```ts
  231. * // Select all users, each user with every pet
  232. * const usersWithPets: { user: User; pets: Pet; }[] = await db.select()
  233. * .from(users)
  234. * .crossJoin(pets)
  235. *
  236. * // Select userId and petId
  237. * const usersIdsAndPetIds: { userId: number; petId: number; }[] = await db.select({
  238. * userId: users.id,
  239. * petId: pets.id,
  240. * })
  241. * .from(users)
  242. * .crossJoin(pets)
  243. *
  244. * // Select userId and petId with use index hint
  245. * const usersIdsAndPetIds: { userId: number; petId: number; }[] = await db.select({
  246. * userId: users.id,
  247. * petId: pets.id,
  248. * })
  249. * .from(users)
  250. * .crossJoin(pets, {
  251. * useIndex: ['pets_owner_id_index']
  252. * })
  253. * ```
  254. */
  255. crossJoin: MySqlCrossJoinFn<this, TDynamic, false>;
  256. /**
  257. * Executes a `cross join lateral` operation by combining rows from two queries into a new table.
  258. *
  259. * A `lateral` join allows the right-hand expression to refer to columns from the left-hand side.
  260. *
  261. * Calling this method retrieves all rows from both main and joined queries, merging all rows from each query.
  262. *
  263. * See docs: {@link https://orm.drizzle.team/docs/joins#cross-join-lateral}
  264. *
  265. * @param table the query to join.
  266. */
  267. crossJoinLateral: MySqlCrossJoinFn<this, TDynamic, true>;
  268. private createSetOperator;
  269. /**
  270. * Adds `union` set operator to the query.
  271. *
  272. * Calling this method will combine the result sets of the `select` statements and remove any duplicate rows that appear across them.
  273. *
  274. * See docs: {@link https://orm.drizzle.team/docs/set-operations#union}
  275. *
  276. * @example
  277. *
  278. * ```ts
  279. * // Select all unique names from customers and users tables
  280. * await db.select({ name: users.name })
  281. * .from(users)
  282. * .union(
  283. * db.select({ name: customers.name }).from(customers)
  284. * );
  285. * // or
  286. * import { union } from 'drizzle-orm/mysql-core'
  287. *
  288. * await union(
  289. * db.select({ name: users.name }).from(users),
  290. * db.select({ name: customers.name }).from(customers)
  291. * );
  292. * ```
  293. */
  294. union: <TValue extends MySqlSetOperatorWithResult<TResult>>(rightSelection: ((setOperators: GetMySqlSetOperators) => SetOperatorRightSelect<TValue, TResult>) | SetOperatorRightSelect<TValue, TResult>) => MySqlSelectWithout<this, TDynamic, MySqlSetOperatorExcludedMethods, true>;
  295. /**
  296. * Adds `union all` set operator to the query.
  297. *
  298. * Calling this method will combine the result-set of the `select` statements and keep all duplicate rows that appear across them.
  299. *
  300. * See docs: {@link https://orm.drizzle.team/docs/set-operations#union-all}
  301. *
  302. * @example
  303. *
  304. * ```ts
  305. * // Select all transaction ids from both online and in-store sales
  306. * await db.select({ transaction: onlineSales.transactionId })
  307. * .from(onlineSales)
  308. * .unionAll(
  309. * db.select({ transaction: inStoreSales.transactionId }).from(inStoreSales)
  310. * );
  311. * // or
  312. * import { unionAll } from 'drizzle-orm/mysql-core'
  313. *
  314. * await unionAll(
  315. * db.select({ transaction: onlineSales.transactionId }).from(onlineSales),
  316. * db.select({ transaction: inStoreSales.transactionId }).from(inStoreSales)
  317. * );
  318. * ```
  319. */
  320. unionAll: <TValue extends MySqlSetOperatorWithResult<TResult>>(rightSelection: ((setOperators: GetMySqlSetOperators) => SetOperatorRightSelect<TValue, TResult>) | SetOperatorRightSelect<TValue, TResult>) => MySqlSelectWithout<this, TDynamic, MySqlSetOperatorExcludedMethods, true>;
  321. /**
  322. * Adds `intersect` set operator to the query.
  323. *
  324. * Calling this method will retain only the rows that are present in both result sets and eliminate duplicates.
  325. *
  326. * See docs: {@link https://orm.drizzle.team/docs/set-operations#intersect}
  327. *
  328. * @example
  329. *
  330. * ```ts
  331. * // Select course names that are offered in both departments A and B
  332. * await db.select({ courseName: depA.courseName })
  333. * .from(depA)
  334. * .intersect(
  335. * db.select({ courseName: depB.courseName }).from(depB)
  336. * );
  337. * // or
  338. * import { intersect } from 'drizzle-orm/mysql-core'
  339. *
  340. * await intersect(
  341. * db.select({ courseName: depA.courseName }).from(depA),
  342. * db.select({ courseName: depB.courseName }).from(depB)
  343. * );
  344. * ```
  345. */
  346. intersect: <TValue extends MySqlSetOperatorWithResult<TResult>>(rightSelection: ((setOperators: GetMySqlSetOperators) => SetOperatorRightSelect<TValue, TResult>) | SetOperatorRightSelect<TValue, TResult>) => MySqlSelectWithout<this, TDynamic, MySqlSetOperatorExcludedMethods, true>;
  347. /**
  348. * Adds `intersect all` set operator to the query.
  349. *
  350. * Calling this method will retain only the rows that are present in both result sets including all duplicates.
  351. *
  352. * See docs: {@link https://orm.drizzle.team/docs/set-operations#intersect-all}
  353. *
  354. * @example
  355. *
  356. * ```ts
  357. * // Select all products and quantities that are ordered by both regular and VIP customers
  358. * await db.select({
  359. * productId: regularCustomerOrders.productId,
  360. * quantityOrdered: regularCustomerOrders.quantityOrdered
  361. * })
  362. * .from(regularCustomerOrders)
  363. * .intersectAll(
  364. * db.select({
  365. * productId: vipCustomerOrders.productId,
  366. * quantityOrdered: vipCustomerOrders.quantityOrdered
  367. * })
  368. * .from(vipCustomerOrders)
  369. * );
  370. * // or
  371. * import { intersectAll } from 'drizzle-orm/mysql-core'
  372. *
  373. * await intersectAll(
  374. * db.select({
  375. * productId: regularCustomerOrders.productId,
  376. * quantityOrdered: regularCustomerOrders.quantityOrdered
  377. * })
  378. * .from(regularCustomerOrders),
  379. * db.select({
  380. * productId: vipCustomerOrders.productId,
  381. * quantityOrdered: vipCustomerOrders.quantityOrdered
  382. * })
  383. * .from(vipCustomerOrders)
  384. * );
  385. * ```
  386. */
  387. intersectAll: <TValue extends MySqlSetOperatorWithResult<TResult>>(rightSelection: ((setOperators: GetMySqlSetOperators) => SetOperatorRightSelect<TValue, TResult>) | SetOperatorRightSelect<TValue, TResult>) => MySqlSelectWithout<this, TDynamic, MySqlSetOperatorExcludedMethods, true>;
  388. /**
  389. * Adds `except` set operator to the query.
  390. *
  391. * Calling this method will retrieve all unique rows from the left query, except for the rows that are present in the result set of the right query.
  392. *
  393. * See docs: {@link https://orm.drizzle.team/docs/set-operations#except}
  394. *
  395. * @example
  396. *
  397. * ```ts
  398. * // Select all courses offered in department A but not in department B
  399. * await db.select({ courseName: depA.courseName })
  400. * .from(depA)
  401. * .except(
  402. * db.select({ courseName: depB.courseName }).from(depB)
  403. * );
  404. * // or
  405. * import { except } from 'drizzle-orm/mysql-core'
  406. *
  407. * await except(
  408. * db.select({ courseName: depA.courseName }).from(depA),
  409. * db.select({ courseName: depB.courseName }).from(depB)
  410. * );
  411. * ```
  412. */
  413. except: <TValue extends MySqlSetOperatorWithResult<TResult>>(rightSelection: ((setOperators: GetMySqlSetOperators) => SetOperatorRightSelect<TValue, TResult>) | SetOperatorRightSelect<TValue, TResult>) => MySqlSelectWithout<this, TDynamic, MySqlSetOperatorExcludedMethods, true>;
  414. /**
  415. * Adds `except all` set operator to the query.
  416. *
  417. * Calling this method will retrieve all rows from the left query, except for the rows that are present in the result set of the right query.
  418. *
  419. * See docs: {@link https://orm.drizzle.team/docs/set-operations#except-all}
  420. *
  421. * @example
  422. *
  423. * ```ts
  424. * // Select all products that are ordered by regular customers but not by VIP customers
  425. * await db.select({
  426. * productId: regularCustomerOrders.productId,
  427. * quantityOrdered: regularCustomerOrders.quantityOrdered,
  428. * })
  429. * .from(regularCustomerOrders)
  430. * .exceptAll(
  431. * db.select({
  432. * productId: vipCustomerOrders.productId,
  433. * quantityOrdered: vipCustomerOrders.quantityOrdered,
  434. * })
  435. * .from(vipCustomerOrders)
  436. * );
  437. * // or
  438. * import { exceptAll } from 'drizzle-orm/mysql-core'
  439. *
  440. * await exceptAll(
  441. * db.select({
  442. * productId: regularCustomerOrders.productId,
  443. * quantityOrdered: regularCustomerOrders.quantityOrdered
  444. * })
  445. * .from(regularCustomerOrders),
  446. * db.select({
  447. * productId: vipCustomerOrders.productId,
  448. * quantityOrdered: vipCustomerOrders.quantityOrdered
  449. * })
  450. * .from(vipCustomerOrders)
  451. * );
  452. * ```
  453. */
  454. exceptAll: <TValue extends MySqlSetOperatorWithResult<TResult>>(rightSelection: ((setOperators: GetMySqlSetOperators) => SetOperatorRightSelect<TValue, TResult>) | SetOperatorRightSelect<TValue, TResult>) => MySqlSelectWithout<this, TDynamic, MySqlSetOperatorExcludedMethods, true>;
  455. /**
  456. * Adds a `where` clause to the query.
  457. *
  458. * Calling this method will select only those rows that fulfill a specified condition.
  459. *
  460. * See docs: {@link https://orm.drizzle.team/docs/select#filtering}
  461. *
  462. * @param where the `where` clause.
  463. *
  464. * @example
  465. * You can use conditional operators and `sql function` to filter the rows to be selected.
  466. *
  467. * ```ts
  468. * // Select all cars with green color
  469. * await db.select().from(cars).where(eq(cars.color, 'green'));
  470. * // or
  471. * await db.select().from(cars).where(sql`${cars.color} = 'green'`)
  472. * ```
  473. *
  474. * You can logically combine conditional operators with `and()` and `or()` operators:
  475. *
  476. * ```ts
  477. * // Select all BMW cars with a green color
  478. * await db.select().from(cars).where(and(eq(cars.color, 'green'), eq(cars.brand, 'BMW')));
  479. *
  480. * // Select all cars with the green or blue color
  481. * await db.select().from(cars).where(or(eq(cars.color, 'green'), eq(cars.color, 'blue')));
  482. * ```
  483. */
  484. where(where: ((aliases: this['_']['selection']) => SQL | undefined) | SQL | undefined): MySqlSelectWithout<this, TDynamic, 'where'>;
  485. /**
  486. * Adds a `having` clause to the query.
  487. *
  488. * Calling this method will select only those rows that fulfill a specified condition. It is typically used with aggregate functions to filter the aggregated data based on a specified condition.
  489. *
  490. * See docs: {@link https://orm.drizzle.team/docs/select#aggregations}
  491. *
  492. * @param having the `having` clause.
  493. *
  494. * @example
  495. *
  496. * ```ts
  497. * // Select all brands with more than one car
  498. * await db.select({
  499. * brand: cars.brand,
  500. * count: sql<number>`cast(count(${cars.id}) as int)`,
  501. * })
  502. * .from(cars)
  503. * .groupBy(cars.brand)
  504. * .having(({ count }) => gt(count, 1));
  505. * ```
  506. */
  507. having(having: ((aliases: this['_']['selection']) => SQL | undefined) | SQL | undefined): MySqlSelectWithout<this, TDynamic, 'having'>;
  508. /**
  509. * Adds a `group by` clause to the query.
  510. *
  511. * Calling this method will group rows that have the same values into summary rows, often used for aggregation purposes.
  512. *
  513. * See docs: {@link https://orm.drizzle.team/docs/select#aggregations}
  514. *
  515. * @example
  516. *
  517. * ```ts
  518. * // Group and count people by their last names
  519. * await db.select({
  520. * lastName: people.lastName,
  521. * count: sql<number>`cast(count(*) as int)`
  522. * })
  523. * .from(people)
  524. * .groupBy(people.lastName);
  525. * ```
  526. */
  527. groupBy(builder: (aliases: this['_']['selection']) => ValueOrArray<MySqlColumn | SQL | SQL.Aliased>): MySqlSelectWithout<this, TDynamic, 'groupBy'>;
  528. groupBy(...columns: (MySqlColumn | SQL | SQL.Aliased)[]): MySqlSelectWithout<this, TDynamic, 'groupBy'>;
  529. /**
  530. * Adds an `order by` clause to the query.
  531. *
  532. * Calling this method will sort the result-set in ascending or descending order. By default, the sort order is ascending.
  533. *
  534. * See docs: {@link https://orm.drizzle.team/docs/select#order-by}
  535. *
  536. * @example
  537. *
  538. * ```
  539. * // Select cars ordered by year
  540. * await db.select().from(cars).orderBy(cars.year);
  541. * ```
  542. *
  543. * You can specify whether results are in ascending or descending order with the `asc()` and `desc()` operators.
  544. *
  545. * ```ts
  546. * // Select cars ordered by year in descending order
  547. * await db.select().from(cars).orderBy(desc(cars.year));
  548. *
  549. * // Select cars ordered by year and price
  550. * await db.select().from(cars).orderBy(asc(cars.year), desc(cars.price));
  551. * ```
  552. */
  553. orderBy(builder: (aliases: this['_']['selection']) => ValueOrArray<MySqlColumn | SQL | SQL.Aliased>): MySqlSelectWithout<this, TDynamic, 'orderBy'>;
  554. orderBy(...columns: (MySqlColumn | SQL | SQL.Aliased)[]): MySqlSelectWithout<this, TDynamic, 'orderBy'>;
  555. /**
  556. * Adds a `limit` clause to the query.
  557. *
  558. * Calling this method will set the maximum number of rows that will be returned by this query.
  559. *
  560. * See docs: {@link https://orm.drizzle.team/docs/select#limit--offset}
  561. *
  562. * @param limit the `limit` clause.
  563. *
  564. * @example
  565. *
  566. * ```ts
  567. * // Get the first 10 people from this query.
  568. * await db.select().from(people).limit(10);
  569. * ```
  570. */
  571. limit(limit: number | Placeholder): MySqlSelectWithout<this, TDynamic, 'limit'>;
  572. /**
  573. * Adds an `offset` clause to the query.
  574. *
  575. * Calling this method will skip a number of rows when returning results from this query.
  576. *
  577. * See docs: {@link https://orm.drizzle.team/docs/select#limit--offset}
  578. *
  579. * @param offset the `offset` clause.
  580. *
  581. * @example
  582. *
  583. * ```ts
  584. * // Get the 10th-20th people from this query.
  585. * await db.select().from(people).offset(10).limit(10);
  586. * ```
  587. */
  588. offset(offset: number | Placeholder): MySqlSelectWithout<this, TDynamic, 'offset'>;
  589. /**
  590. * Adds a `for` clause to the query.
  591. *
  592. * Calling this method will specify a lock strength for this query that controls how strictly it acquires exclusive access to the rows being queried.
  593. *
  594. * See docs: {@link https://dev.mysql.com/doc/refman/8.0/en/innodb-locking-reads.html}
  595. *
  596. * @param strength the lock strength.
  597. * @param config the lock configuration.
  598. */
  599. for(strength: LockStrength, config?: LockConfig): MySqlSelectWithout<this, TDynamic, 'for'>;
  600. toSQL(): Query;
  601. as<TAlias extends string>(alias: TAlias): SubqueryWithSelection<this['_']['selectedFields'], TAlias>;
  602. $dynamic(): MySqlSelectDynamic<this>;
  603. $withCache(config?: {
  604. config?: CacheConfig;
  605. tag?: string;
  606. autoInvalidate?: boolean;
  607. } | false): this;
  608. }
  609. export interface MySqlSelectBase<TTableName extends string | undefined, TSelection extends ColumnsSelection, TSelectMode extends SelectMode, TPreparedQueryHKT extends PreparedQueryHKTBase, TNullabilityMap extends Record<string, JoinNullability> = TTableName extends string ? Record<TTableName, 'not-null'> : {}, TDynamic extends boolean = false, TExcludedMethods extends string = never, TResult extends any[] = SelectResult<TSelection, TSelectMode, TNullabilityMap>[], TSelectedFields extends ColumnsSelection = BuildSubquerySelection<TSelection, TNullabilityMap>> extends MySqlSelectQueryBuilderBase<MySqlSelectHKT, TTableName, TSelection, TSelectMode, TPreparedQueryHKT, TNullabilityMap, TDynamic, TExcludedMethods, TResult, TSelectedFields>, QueryPromise<TResult> {
  610. }
  611. export declare class MySqlSelectBase<TTableName extends string | undefined, TSelection, TSelectMode extends SelectMode, TPreparedQueryHKT extends PreparedQueryHKTBase, TNullabilityMap extends Record<string, JoinNullability> = TTableName extends string ? Record<TTableName, 'not-null'> : {}, TDynamic extends boolean = false, TExcludedMethods extends string = never, TResult = SelectResult<TSelection, TSelectMode, TNullabilityMap>[], TSelectedFields = BuildSubquerySelection<TSelection, TNullabilityMap>> extends MySqlSelectQueryBuilderBase<MySqlSelectHKT, TTableName, TSelection, TSelectMode, TPreparedQueryHKT, TNullabilityMap, TDynamic, TExcludedMethods, TResult, TSelectedFields> {
  612. static readonly [entityKind]: string;
  613. prepare(): MySqlSelectPrepare<this>;
  614. execute: ReturnType<this["prepare"]>["execute"];
  615. private createIterator;
  616. iterator: ReturnType<this["prepare"]>["iterator"];
  617. }
  618. /**
  619. * Adds `union` set operator to the query.
  620. *
  621. * Calling this method will combine the result sets of the `select` statements and remove any duplicate rows that appear across them.
  622. *
  623. * See docs: {@link https://orm.drizzle.team/docs/set-operations#union}
  624. *
  625. * @example
  626. *
  627. * ```ts
  628. * // Select all unique names from customers and users tables
  629. * import { union } from 'drizzle-orm/mysql-core'
  630. *
  631. * await union(
  632. * db.select({ name: users.name }).from(users),
  633. * db.select({ name: customers.name }).from(customers)
  634. * );
  635. * // or
  636. * await db.select({ name: users.name })
  637. * .from(users)
  638. * .union(
  639. * db.select({ name: customers.name }).from(customers)
  640. * );
  641. * ```
  642. */
  643. export declare const union: MySqlCreateSetOperatorFn;
  644. /**
  645. * Adds `union all` set operator to the query.
  646. *
  647. * Calling this method will combine the result-set of the `select` statements and keep all duplicate rows that appear across them.
  648. *
  649. * See docs: {@link https://orm.drizzle.team/docs/set-operations#union-all}
  650. *
  651. * @example
  652. *
  653. * ```ts
  654. * // Select all transaction ids from both online and in-store sales
  655. * import { unionAll } from 'drizzle-orm/mysql-core'
  656. *
  657. * await unionAll(
  658. * db.select({ transaction: onlineSales.transactionId }).from(onlineSales),
  659. * db.select({ transaction: inStoreSales.transactionId }).from(inStoreSales)
  660. * );
  661. * // or
  662. * await db.select({ transaction: onlineSales.transactionId })
  663. * .from(onlineSales)
  664. * .unionAll(
  665. * db.select({ transaction: inStoreSales.transactionId }).from(inStoreSales)
  666. * );
  667. * ```
  668. */
  669. export declare const unionAll: MySqlCreateSetOperatorFn;
  670. /**
  671. * Adds `intersect` set operator to the query.
  672. *
  673. * Calling this method will retain only the rows that are present in both result sets and eliminate duplicates.
  674. *
  675. * See docs: {@link https://orm.drizzle.team/docs/set-operations#intersect}
  676. *
  677. * @example
  678. *
  679. * ```ts
  680. * // Select course names that are offered in both departments A and B
  681. * import { intersect } from 'drizzle-orm/mysql-core'
  682. *
  683. * await intersect(
  684. * db.select({ courseName: depA.courseName }).from(depA),
  685. * db.select({ courseName: depB.courseName }).from(depB)
  686. * );
  687. * // or
  688. * await db.select({ courseName: depA.courseName })
  689. * .from(depA)
  690. * .intersect(
  691. * db.select({ courseName: depB.courseName }).from(depB)
  692. * );
  693. * ```
  694. */
  695. export declare const intersect: MySqlCreateSetOperatorFn;
  696. /**
  697. * Adds `intersect all` set operator to the query.
  698. *
  699. * Calling this method will retain only the rows that are present in both result sets including all duplicates.
  700. *
  701. * See docs: {@link https://orm.drizzle.team/docs/set-operations#intersect-all}
  702. *
  703. * @example
  704. *
  705. * ```ts
  706. * // Select all products and quantities that are ordered by both regular and VIP customers
  707. * import { intersectAll } from 'drizzle-orm/mysql-core'
  708. *
  709. * await intersectAll(
  710. * db.select({
  711. * productId: regularCustomerOrders.productId,
  712. * quantityOrdered: regularCustomerOrders.quantityOrdered
  713. * })
  714. * .from(regularCustomerOrders),
  715. * db.select({
  716. * productId: vipCustomerOrders.productId,
  717. * quantityOrdered: vipCustomerOrders.quantityOrdered
  718. * })
  719. * .from(vipCustomerOrders)
  720. * );
  721. * // or
  722. * await db.select({
  723. * productId: regularCustomerOrders.productId,
  724. * quantityOrdered: regularCustomerOrders.quantityOrdered
  725. * })
  726. * .from(regularCustomerOrders)
  727. * .intersectAll(
  728. * db.select({
  729. * productId: vipCustomerOrders.productId,
  730. * quantityOrdered: vipCustomerOrders.quantityOrdered
  731. * })
  732. * .from(vipCustomerOrders)
  733. * );
  734. * ```
  735. */
  736. export declare const intersectAll: MySqlCreateSetOperatorFn;
  737. /**
  738. * Adds `except` set operator to the query.
  739. *
  740. * Calling this method will retrieve all unique rows from the left query, except for the rows that are present in the result set of the right query.
  741. *
  742. * See docs: {@link https://orm.drizzle.team/docs/set-operations#except}
  743. *
  744. * @example
  745. *
  746. * ```ts
  747. * // Select all courses offered in department A but not in department B
  748. * import { except } from 'drizzle-orm/mysql-core'
  749. *
  750. * await except(
  751. * db.select({ courseName: depA.courseName }).from(depA),
  752. * db.select({ courseName: depB.courseName }).from(depB)
  753. * );
  754. * // or
  755. * await db.select({ courseName: depA.courseName })
  756. * .from(depA)
  757. * .except(
  758. * db.select({ courseName: depB.courseName }).from(depB)
  759. * );
  760. * ```
  761. */
  762. export declare const except: MySqlCreateSetOperatorFn;
  763. /**
  764. * Adds `except all` set operator to the query.
  765. *
  766. * Calling this method will retrieve all rows from the left query, except for the rows that are present in the result set of the right query.
  767. *
  768. * See docs: {@link https://orm.drizzle.team/docs/set-operations#except-all}
  769. *
  770. * @example
  771. *
  772. * ```ts
  773. * // Select all products that are ordered by regular customers but not by VIP customers
  774. * import { exceptAll } from 'drizzle-orm/mysql-core'
  775. *
  776. * await exceptAll(
  777. * db.select({
  778. * productId: regularCustomerOrders.productId,
  779. * quantityOrdered: regularCustomerOrders.quantityOrdered
  780. * })
  781. * .from(regularCustomerOrders),
  782. * db.select({
  783. * productId: vipCustomerOrders.productId,
  784. * quantityOrdered: vipCustomerOrders.quantityOrdered
  785. * })
  786. * .from(vipCustomerOrders)
  787. * );
  788. * // or
  789. * await db.select({
  790. * productId: regularCustomerOrders.productId,
  791. * quantityOrdered: regularCustomerOrders.quantityOrdered,
  792. * })
  793. * .from(regularCustomerOrders)
  794. * .exceptAll(
  795. * db.select({
  796. * productId: vipCustomerOrders.productId,
  797. * quantityOrdered: vipCustomerOrders.quantityOrdered,
  798. * })
  799. * .from(vipCustomerOrders)
  800. * );
  801. * ```
  802. */
  803. export declare const exceptAll: MySqlCreateSetOperatorFn;