Welcome Guest, Not a member yet? Register   Sign In
SQL query with 1 fixed and 2 OR conditions
#1

Hi all,
I am trying to find a clean way (without using raw SQL) to do this:
SELECT * FROM table WHERE user_id = 123 AND (role_id IN (1,2,3,4) OR role_id IS NULL)
So I always need to check the user_id, but the role_id may be in the list or NULL.
PHP Code:
$this->usersModel
            
->where('user_id'$userId)
            ->whereIn('role_id', [1,2,3,4])
            ->orWHere('role_id'null)
            ->findAll(); 

But this results in: SELECT * FROM table WHERE user_id = 123 AND role_id IN (1,2,3,4) OR role_id IS NULL.
So it will show also the records where role_id is null, regardless of the user_id.
Any ideas how to do this?
Reply


Messages In This Thread
SQL query with 1 fixed and 2 OR conditions - by sjender - 11-21-2023, 08:42 AM



Theme © iAndrew 2016 - Forum software by © MyBB