Skip to content

Commit 3df2481

Browse files
committed
Add WITH example
1 parent 7126cd1 commit 3df2481

1 file changed

Lines changed: 33 additions & 0 deletions

File tree

guide/index.rst

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff 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

0 commit comments

Comments
 (0)