mysql - SQL sub queries with WHERE NOT EXISTS -


i want select characters character has not challenged in past 24 hours.

 select * challenges  usercharid = 642 , chaltime > date_sub(curdate(), interval 1 day) 

this returns few rows challenges character has initiated in past day

select characterid characters  not exists (select * challenges                    usercharid = '610'                      , chaltime > date_sub(curdate(), interval 1 day)) 

am using not exists wrong?

am using not exists wrong?

yes. want using not in rather not exists. if use not exists , non-existential sub-query returns rows, condition false , no data returned main query. if no rows returned, condition true , rows returned main query (since, in example, there no other criteria in main query). often, sub-query in not exists correlated sub-query, sub-query has evaluated each row. here, don't have correlated sub-query (which performance). query means 'return information characters unless there exists character who's been challenged in last 1 day nominated user'.

(in analysis, i've quietly changed sql usercharid compared string, , value '642' specifically.)

select characters character [has] challenged in past 24 hours:

select *   challenges  usercharid = '642'    , chaltime > date_sub(curdate(), interval 1 day) 

this returns few rows challenges character has initiated in past day.

so, find people have not challenged, need select users except in list have challenged, translates to:

select characterid   characters   usercharid not in        (select usercharid           challenges          usercharid = '642'            , chaltime > date_sub(curdate(), interval 1 day)        ) 

this should give (possibly rather large) list of characters you've not challenged within last 24 hours.


Comments

Popular posts from this blog

c# - SVN Error : "svnadmin: E205000: Too many arguments" -

c# - Copy ObservableCollection to another ObservableCollection -

All overlapping substrings matching a java regex -