SQL Select UserIds with a login on two different days -


it's kind of embarrassing, sounds pretty easy want, dont solution (and didnt find meaningful title, sorry if doesnt fit problem)

imagine 2 tables in store user-infos , sessions user had system:

table.users (uid, registeredat, gender, locale, ...) table.sessions (sid, userid, logindate, ...) 

what want list of unique user id's had @ least 1 session yesterday , today (eg 2013-08-26 , 2013-08-27). thing is, user can have more 1 session per day , dont want multiple userid's in results. tried lots of things think i'm not expert enough done.

my problem understand how can select userid after comparing 2 rows according userid , date_yesterday , date_today. i'm curious solution experts have that.

i hope description clear enough. thank in advance

you use exists follows (each subquery ensures there login 1 day):

select  *    users u   exists          (   select  1                 sessions s                s.userid = u.uid              ,     s.logindate >= '20130826'              ,     s.logindate < '20130827'         ) ,     exists          (   select  1                 sessions s                s.userid = u.uid              ,     s.logindate >= '20130827'              ,     s.logindate < '20130828'         ); 

or use group by/having, although exact solution dbms specific (you need strip time date logindate)

select  u.*    users u         inner join         (   select  s.userid                sessions s               s.logindate >= '20130826'             ,     s.logindate < '20130828'             group s.userid             having count(distinct cast(s.logindate date)) = 2 -- 2 distinct login dates         ) s             on s.userid = u.uid; 

depending on dbms may have replace cast(s.logindate date) similar.


Comments

Popular posts from this blog

Unable to remove the www from url on https using .htaccess -