WHERE Clause, AND & OR operators

WHERE Clause, AND & OR operators

tkphuongtkphuong Posts: 5Questions: 3Answers: 0
edited March 2017 in Editor

Hi, I'm trying to write a WHERE clause with OR and AND operators but have been unsuccessful. Please help. I'm looking for way to filter sql data for rows that have:
1. the specified ID, and
2. Not inactive, and
3. Either has value A, B, C, or D

The code below didn't work as well as several other variation that I tried.

->where( function ( $q ) {
    $q->where('ID',$_GET['ID']);
        ->where('inactive','0');
        ->or_where( function ( $r ) {
        $r->where( 'Type', 'A' );
            $r->where( 'Type', 'B' );
            $r->where( 'Type', 'C' );
        $r->where( 'Type', 'D' );
        } );
} );

Thank you.

Answers

  • allanallan Posts: 61,695Questions: 1Answers: 10,102 Site admin
    edited March 2017

    Try:

    ->where( function ( $q ) {
        $q
            ->where('ID',$_GET['ID']);
            ->where('inactive','0');
            ->where( function ( $r ) {
                $r->or_where( 'Type', 'A' );
                $r->or_where( 'Type', 'B' );
                $r->or_where( 'Type', 'C' );
                $r->or_where( 'Type', 'D' );
            } );
    } );
    

    Allan

  • tkphuongtkphuong Posts: 5Questions: 3Answers: 0

    Thanks for the suggestion Allan, but that still doesn't work. I'm still getting the invalid JSON response error. :/

  • allanallan Posts: 61,695Questions: 1Answers: 10,102 Site admin

    Can you show me what the server is responding with - it is probably an error message if it isn't valid JSON?

    Allan

This discussion has been closed.