File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -396,6 +396,39 @@ The SQL statement below is the one executed in the example above:
396396 WITH
397397####
398398
399+ WITH allows you to refer to a subquery expression many times in a query, as if
400+ having a temporary table that only exists for the duration of a query.
401+
402+ .. code-block :: php
403+
404+ $result = $database->with()->reference('t', function (Select $select) {
405+ return $select->expressions('a')
406+ ->from('t1')
407+ ->whereGreaterThanOrEqual('b', 'c')
408+ ->sql();
409+ })->select(function (Select $select) {
410+ return $select->from('t2', 't')
411+ ->whereEqual(
412+ 't2.c',
413+ fn (Database $db) => $db->protectIdentifier('t.a')
414+ )->sql();
415+ })->run();
416+
417+ The code above will build and execute the following statement:
418+
419+ .. code-block :: sql
420+
421+ WITH
422+ `t` AS (SELECT
423+ `a`
424+ FROM `t1`
425+ WHERE `b` >= 'c'
426+ )
427+ SELECT
428+ *
429+ FROM `t2`, `t`
430+ WHERE `t2`.`c` = (`t`.`a`)
431+
399432 LOAD DATA
400433#########
401434
You can’t perform that action at this time.
0 commit comments