Skip to content

Conversation

@skipperbent
Copy link

This feature will add support for nested queries.

Please not this example is taken from a live site, but it should provide an idea of what the changes are about.

Example (psuedo code):

$userDataQuery = $this->newQuery('user_data')
            ->getQuery()
            ->select('user_id')
            ->where('user_id', '=', \QB::raw('user.id'))
            ->where('value', 'LIKE', '%'. str_replace('%', '%%', $query) .'%')
            ->limit(1);

$otherQuery->where('username', 'LIKE', '%'.str_replace('%', '%%', $query).'%')
            ->orWhere('id', '=', $userDataQuery);

Output:

SELECT * FROM `user` WHERE `username` LIKE '%value%' OR `id` = (SELECT `user_id` FROM `user_data` WHERE `user_id` = user.id AND `value` LIKE '%value%' LIMIT 1)

@usmanhalalit
Copy link
Owner

Appreciate the PR Simon, but have you checked it?
https://github.com/usmanhalalit/pixie#sub-queries-and-nested-queries

@skipperbent
Copy link
Author

What! It's already there! What the hell - sorry about wasting your time, was sure that i'd read the entire documentation, but must have missed that section. 👍

But can I use those subqueries in my where statements like this:

... WHERE `id` IN (SELECT id FROM x) AND ...

Would it then just be something like:

->where('user_id', '=', \QB::subQuery($subQuery)) ?

@skipperbent
Copy link
Author

It would be much cleaner though if the subQuery happened behind-the-scenes in the QueryBuilderHandler.php, so you don't have to know that the \QB::subQuery method exists.

My PR allows you to extract the sub-query QueryBuilder object back out of the original QueryBuilder-object instead of just turning it into a raw-query.

What do you think?

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