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

Few limit issue #68

Closed
michalbundyra opened this issue Jan 16, 2020 · 1 comment
Closed

Few limit issue #68

michalbundyra opened this issue Jan 16, 2020 · 1 comment

Comments

@michalbundyra
Copy link
Member

Briefly: SQL queries fails when few limit statements is used (sub-select, maybe unions)

Context: PDO Mysql driver

Expected SQL (simplified):

SELECT (SELECT column FROM foo LIMIT 1) AS column 
FROM bar 
LIMIT 99

PHP code to build them

$subSelect = new Select('foo');
$subSelect->columns(['column'])->limit(1);

$select = new Select('bar');
$select->columns(['column' => $subSelect])->limit(99);

$statement = $sql->prepareStatementForSqlObject($select);
print_r($statement);

Output (simplified)

[sql] => SELECT (SELECT `foo`.`column` AS `column` FROM `foo` LIMIT :limit) AS `column` FROM `bar` LIMIT :limit
[parameterContainer] => Zend\Db\Adapter\ParameterContainer Object
        (
            [data] => Array
                (
                    [limit] => 99
                )

            [positions] => Array
                (
                    [0] => limit
                )
        )

Same value sent to mysql for both limit statements:

SELECT (SELECT column FROM foo LIMIT 99) AS column 
FROM bar 
LIMIT 99

Test: autowp/zend-db@95efe84

The only workaround is to create sub-select with SQL-string


Originally posted by @autowp at zendframework/zend-db#262

@samsonasik
Copy link
Member

I tried the code and got the following output:

[sql:protected] => SELECT (SELECT `foo`.`column` AS `column` FROM `foo` LIMIT :subselect1limit) AS `column` FROM `bar` LIMIT :limit
[parameterContainer:protected] => Laminas\Db\Adapter\ParameterContainer Object
        (
            [data:protected] => Array
                (
                    [subselect1limit] => 1
                    [limit] => 99
                )

            [positions:protected] => Array
                (
                    [0] => subselect1limit
                    [1] => limit
                )

            [errata:protected] => Array
                (
                    [subselect1limit] => integer
                    [limit] => integer
                )

            [maxLength:protected] => Array
                (
                )

        )

When printed echo $sql->getSqlStringForSqlObject($select);, it got:

SELECT (SELECT `foo`.`column` AS `column` FROM `foo` LIMIT 1) AS `column` FROM `bar` LIMIT 99

It seems already fixed by PR zendframework/zend-db#300 . Please re-create issue if problem persist.

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

No branches or pull requests

2 participants