-3

I have a users table with many columns however I am trying to use the below 2 columns to fetch the users do not have expired = 1

The query should return only user 2. Any help is much appreciated.

user_id  expired 
1          1
1          0
2          0
2          0
3          1
Dale K
  • 25,246
  • 15
  • 42
  • 71
Rocky3151
  • 67
  • 7

2 Answers2

3

I know you have an answer, but if you are just looking for a list of single user_id's this might work a little more efficiently:

select user_id 
from users
GROUP BY user_id
having MAX(expired) = 0
JMabee
  • 2,230
  • 2
  • 9
  • 13
0

You can do this using not in like below :

select * from users where user_id 
                           not in(select distinct user_id from users where expired = 1);

See SQL Here

Md. Suman Kabir
  • 5,243
  • 5
  • 25
  • 43