pixie icon indicating copy to clipboard operation
pixie copied to clipboard

how to add sub condition in where like `(x=1 or y=1) and (z>0 and z<>1)`

Open kingofnull opened this issue 9 years ago • 1 comments

how can i add sub conditions? for example I want generate a query like this:

SELECT filed FROM table where (x=1 or y=1) and (z>0 and z<>1)

how can i generate it?

kingofnull avatar Oct 05 '16 10:10 kingofnull

I think that you should use code like this below:

        $query = $this->oQuery
            ->table('table ')
            ->select('field')
            ->where(function ($q) {
                $q->Where('x', '=', 1);
                $q->OrWhere('y', '=', 1);
            })
            ->where(function ($q) {
                $q->Where('z', '>', 1);
                $q->Where('z', '<>', 1);
            });

        $queryObj = $query->getQuery();
        //$check = $query->get();
        echo $queryObj->getRawSql();

And response: SELECTfieldFROMtable WHERE (x= 1 ORy= 1) AND (z> 1 ANDz<> 1)

mrcnpdlk avatar Oct 19 '16 21:10 mrcnpdlk