Skip to content

Sub queries#12

Merged
gwillz merged 8 commits intomasterfrom
feat/sub-queries
Nov 10, 2025
Merged

Sub queries#12
gwillz merged 8 commits intomasterfrom
feat/sub-queries

Conversation

@gwillz
Copy link
Copy Markdown
Collaborator

@gwillz gwillz commented Nov 6, 2025

All those fancy things we dreamed about!

This is subquery nested in the FRON statement like SELECT * FROM (...) AS subQuery.

$subQuery = new PdbQuery($pdb);

$query = (new PdbQuery($pdb))
    ->from($subQuery, 'foo');

$subQuery->from('bar')
    ->where(['name' => 'test'])
    ->select('id, category');

Some of the key magic here is that the subQuery isn't built or validated until the parent query is built. Meaning you're free to modify that object after attaching it to the parent.

This is a subquery using the WITH statement, which is somewhat like a temporary/inline VIEW.

$subQuery = $pdb->find('foo')
    ->select('id as _id')
    ->where(['name' => 'test']);

$query = $pdb->find('foo')
   ->innerJoin('sub', '~foo.id = sub._id')
   ->with($subQuery, 'sub');

Union also exists now, but that's pretty boring.

Having said all that. This PR description should go straight into some docs.

The test cases also need some real executed queries. Not just generated SQL.

@gwillz gwillz requested a review from aitken85 November 6, 2025 23:11
@gwillz gwillz merged commit d930f89 into master Nov 10, 2025
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants