Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Build a way to use subqueries for joins #450

Open
1 task
josephmancuso opened this issue May 3, 2021 · 4 comments
Open
1 task

Build a way to use subqueries for joins #450

josephmancuso opened this issue May 3, 2021 · 4 comments
Labels
enhancement A feature that exists, works as intended but needs to be improved hard These issues are geared for people who have contributed to the project a lot

Comments

@josephmancuso
Copy link
Member

josephmancuso commented May 3, 2021

Describe the feature as you'd like to see it
Need a way to build a query like this:

SELECT * FROM `scan_dns` AS `s`
INNER JOIN (
  SELECT x.watch_id, MAX(x.last_scan_at) as last_scan
  FROM `scan_dns` AS `x`
  WHERE `x`.`watch_id` IN (1,2,3,4,5,42)
  GROUP BY `x`.`watch_id`) AS ss
ON `s`.`watch_id` = `ss`.`watch_id` AND `s`.`last_scan_at` = `ss`.`last_scan`

What do we currently have to do now?
I don't believe we have a way to pass a builder to a join to create a subquery

Additional context
Found this query on Stack overflow and would like a way to do this via the query builder, if possible.

Open for discussion

  • Is this a breaking change?
@josephmancuso josephmancuso added the enhancement A feature that exists, works as intended but needs to be improved label May 3, 2021
@josephmancuso
Copy link
Member Author

josephmancuso commented May 3, 2021

This ticket is open for discussion on how this would be implemented with the query builder, if it is implemented at all.

@josephmancuso
Copy link
Member Author

Possibly related, or requires, #436

@josephmancuso
Copy link
Member Author

Maybe propose a new join_sub method? which should take the following signature: join_sub('other_table', 'subquery_alias')

builder.join_sub('scan_dns', 'ss', lambda query: (
    query.select('watch_id').max('x.last_scan_at as last_scan').where_in('watch_id', [1,2,3,4,5,42]).group_by('watch_id')
), 'watch_id', '=', 'ss.watch_id')

Will need to figure out how to do the "AND" there

@josephmancuso
Copy link
Member Author

josephmancuso commented Jun 22, 2021

After we have implemented new joining logic using a new JoinClause class, I think we can do the same thing here using a JoinQuery class.

The example would look something like this:

from masoniteorm.expressions import JoinQuery

clause = (
    JoinQuery('scan_dns as x', as='ss')
    .select('x.watch_id')
    .max('x.last_scan_at as last_scan')
    .where('x.watch_id', [1,2,3,4,5,42])
    .group_by('x.watch_id')

query.join(clause)

This should be fairly simple to implement but a little harder to get the query builder part working unless we had a pass through method and somehow attached a query builder object to the JoinQuery

@josephmancuso josephmancuso added the hard These issues are geared for people who have contributed to the project a lot label Jun 22, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement A feature that exists, works as intended but needs to be improved hard These issues are geared for people who have contributed to the project a lot
Projects
None yet
Development

No branches or pull requests

1 participant