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

💡 Subquery column alias injection #72

Open
gam6itko opened this issue Aug 19, 2022 · 2 comments
Open

💡 Subquery column alias injection #72

gam6itko opened this issue Aug 19, 2022 · 2 comments
Assignees
Labels
type: feature New feature.

Comments

@gam6itko
Copy link
Contributor

I have an idea!

I have the query with subquery columns. I need to make aliases for subquery columns for having;

$result = $database
    ->select([
        'table1.id',
        $database->select('COUNT(1)')->from('table2')->where('inner_id', new Expression('table1.id')), // as cnt1
    ])
    ->from('table1')
    ->fetchAll();

now I need to do weird things like this

$result = $database
    ->select([
        'table1.id',
        new Fragment('('.$database->select('COUNT(1)')->from('table2')->where('inner_id', new Expression('table1.id')).') as cnt1'),
    ])
    ->from('table1')
    ->having('cnt1', 1)
    ->fetchAll();

We need something like ColumnAliasInjection to make it prettier

$result = $database
    ->select([
        'table1.id',
        new ColumnAliasInjection(
            $database->select('COUNT(1)')->from('table2')->where('inner_id', new Expression('table1.id')),
            'cnt1'
        )
    ])
    ->from('table1')
    ->having('cnt1', 1)
    ->fetchAll();
@gam6itko gam6itko added the type: feature New feature. label Aug 19, 2022
@gam6itko gam6itko changed the title 💡 Subquery alias injection 💡 Subquery column alias injection Aug 19, 2022
@AnrDaemon
Copy link

Why not

->select([
        'id' => 'table1.id',
        'cnt1' => new Fragment('('.$database->select('COUNT(1)')->from('table2')->where('inner_id', new Expression('table1.id')).')'),
    ])

etc. ?

@gam6itko
Copy link
Contributor Author

has common traits with #200

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: feature New feature.
Projects
Status: Backlog
Development

No branches or pull requests

3 participants