3

I'm trying to do a query where in the database there's multiple groups in multiple rows, but if it one of the rows meets a certain criteria I want the query to return nothing.

I tried with CASE but that doesnt seem to work something like this

select *, 
    CASE WHEN groupname = 'is_%' and groupname != 'is_banned' THEN false END 
from usersgroups 
WHERE username = 'tu1';

not sure what to do.

Thanks

EDIT:

This is the way I have the database atm

username|usergroup
tu1     |is_user
tu1     |is_banned
tu2     |is_user

so what I'm looking to get is the query to only return values if a user is part of any is_ groups and they arent is_banned, but problem is that they are on different rows

Thanks again

1 Answer 1

2
select *
from usersgroups 
WHERE
    username = 'tu1'
    and exists (
        select 1
        from usersgroups
        where
            username = 'tu1'
            and groupname like 'is_%'
    )
    and not exists (
        select 1
        from usersgroups
        where
            username = 'tu1'
            and groupname = 'is_banned'
    )
Sign up to request clarification or add additional context in comments.

1 Comment

That still returns all values, I explained myself badly, I edited the original post with a better explanation, thanks

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.