Skip to content

Commit 1541d06

Browse files
committed
Feature: Subquery for SELECT
Signed-off-by: Andrey Pyzhikov <5071@mail.ru>
1 parent 2179c95 commit 1541d06

File tree

5 files changed

+53
-0
lines changed

5 files changed

+53
-0
lines changed

system/Database/BaseBuilder.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -445,6 +445,22 @@ public function selectCount(string $select = '', string $alias = '')
445445
return $this->maxMinAvgSum($select, $alias, 'COUNT');
446446
}
447447

448+
/**
449+
* Adds a subquery to the selection
450+
*
451+
* @return static
452+
*/
453+
public function selectSubquery(BaseBuilder $subquery, string $as)
454+
{
455+
if (! $this->isSubquery($subquery)) {
456+
throw new DatabaseException('The BaseBuilder::selectSubquery method expects a BaseBuilder instance');
457+
}
458+
459+
$this->QBSelect[] = $this->buildSubquery($subquery, true, $as);
460+
461+
return $this;
462+
}
463+
448464
/**
449465
* SELECT [MAX|MIN|AVG|SUM|COUNT]()
450466
*

tests/system/Database/Builder/SelectTest.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,4 +246,17 @@ public function testSimpleSelectWithSQLSRV()
246246

247247
$this->assertSame($expected, str_replace("\n", ' ', $builder->getCompiledSelect()));
248248
}
249+
250+
public function testSelectSubquery()
251+
{
252+
$builder = new BaseBuilder('users', $this->db);
253+
$subquery = new BaseBuilder('countries', $this->db);
254+
255+
$subquery->select('name')->where('id', 1);
256+
$builder->select('name')->selectSubquery($subquery, 'country');
257+
258+
$expected = 'SELECT "name", (SELECT "name" FROM "countries" WHERE "id" = 1) AS "country" FROM "users"';
259+
260+
$this->assertSame($expected, str_replace("\n", ' ', $builder->getCompiledSelect()));
261+
}
249262
}

user_guide_src/source/changelogs/v4.2.0.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ Enhancements
2424
- See :ref:`content-security-policy` for details.
2525
- New View Decorators allow modifying the generated HTML prior to caching.
2626
- Added Subqueries in the FROM section. See :ref:`query-builder-from-subquery`.
27+
- Added Subqueries in the SELECT section.
2728
- Added Validation Strict Rules. See :ref:`validation-traditional-and-strict-rules`.
2829
- Added new OCI8 driver for database.
2930
- It can access Oracle Database and supports SQL and PL/SQL statements.

user_guide_src/source/database/query_builder.rst

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,13 @@ the resulting field.
167167
.. literalinclude:: query_builder/014.php
168168
:lines: 2-
169169

170+
**$builder->selectSubquery()**
171+
172+
Adds a subquery to the SELECT section.
173+
174+
.. literalinclude:: query_builder/098.php
175+
:lines: 2-
176+
170177
From
171178
====
172179

@@ -1066,6 +1073,15 @@ Class Reference
10661073

10671074
Adds a ``SELECT COUNT(field)`` clause to a query.
10681075

1076+
.. php:method:: selectSubquery(BaseBuilder $subquery, string $as)
1077+
1078+
:param string $subquery: Instance of BaseBuilder
1079+
:param string $as: Alias for the resulting value name
1080+
:returns: ``BaseBuilder`` instance (method chaining)
1081+
:rtype: ``BaseBuilder``
1082+
1083+
Adds a subquery to the selection
1084+
10691085
.. php:method:: distinct([$val = true])
10701086
10711087
:param bool $val: Desired value of the "distinct" flag
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?php
2+
3+
$subquery = $db->table('countries')->select('name')->where('id', 1);
4+
$builder = $db->table('users')->select('name')->selectSubquery($subquery, 'country');
5+
$query = $builder->get();
6+
7+
// Produces: SELECT `name`, (SELECT `name` FROM `countries` WHERE `id` = 1) AS `country` FROM `users`

0 commit comments

Comments
 (0)