php-sql-query-builder icon indicating copy to clipboard operation
php-sql-query-builder copied to clipboard

Nested subWhere's in complex where statements?

Open LincolnLex44 opened this issue 7 years ago • 2 comments

I need to recreate this sql statement with the builder, but really struggling with creating a group of subwhere statements.

WHERE website_listing_priority IS NOT NULL
AND (
    (
        duration_magnitude <= 90 AND 
        duration_unit = 'Days'
     ) OR (
        duration_magnitude <= 12 AND 
        duration_unit = 'Weeks'
    ) OR (
        duration_magnitude <= 3 AND 
        duration_unit = 'Months'
    ) OR (
         duration_magnitude = 0 AND duration_unit = 'Years'
    )
)

Is there any way to handle this? Initially I thought I could do something like this

    $sql_ob->where()
        ->subWhere('OR')
            ->lessThanOrEqual('duration_magnitude', 90)
            ->lessThanOrEqual('duration_unit', 'Days')
        ->subWhere('OR')
            ->lessThanOrEqual('duration_magnitude', 12)
            ->lessThanOrEqual('duration_unit', 'Weeks')
        ->subWhere('OR')
            ->lessThanOrEqual('duration_magnitude', 3)
            ->lessThanOrEqual('duration_unit', 'Months')
        ->subWhere('OR')
            ->equals('duration_magnitude', 0)
            ->lessThanOrEqual('duration_unit', 'Years');

but this simply nests the subwhere's within each other e.g.

And ( Or ( Or ( Or ( ) ) ) )

How would I nest each of the subwhere's within the first Where statement?

LincolnLex44 avatar May 03 '18 13:05 LincolnLex44

Woow this really getting complicated @riseoflex88 , I suggest you use normal mySql query here

marcus-hiles avatar Mar 29 '19 11:03 marcus-hiles

i faced a similar problem... subwheres are not helpful in complex queries, which anyone will have to encounter at some point. In SQL it's easy to just add a parenthesis, the equivalent here would have been to have a method like beginGroup() and endGroup()

usernotnull avatar May 02 '20 00:05 usernotnull