mysql - SQL Find All user that have two history in a certain order -
i'm looking sql request can't find in internet (and didn't found solution myself).
i have 2 different table user , history , table user_history link 2 tables.
for example :
user
id name 1 john 2 edie 3 france 4 gabriel
5 ellen
history
id date_entered type 1 2017-07-01 36 2 2017-07-02 52 3 2017-07-03 25 4 2017-07-04 69 5 2017-07-05 85 6 2017-07-06 74 7 2017-07-07 45 8 2017-07-08 85 9 2017-07-09 25 10 2017-07-10 78
user_history
id id_user id_history 1 1 1 2 1 2 3 1 3 4 1 4 5 2 5 6 2 6 7 1 7 8 1 8 9 2 9 10 1 10
in example, history made user 1 , 2 (user 2 have history 5,6 , 9).
so question :
what sql request me users have in history history type 25 , days later history type 85 ?
in example, user 1 (john) ok because has history type 25 on 2017-07-03 , history type 85 on 2017-07-08. user 2 (edie) not ok because if has history 25 , 85, first 1 85 , 25.
is clear ? can me please ?
you need join
twice history
table, e.g.:
select h1.id_user ( select u.id_user, h.date_entered user_history u join history h on u.id_history = h.id h.type = 25) h1 join ( select u.id_user, h.date_entered user_history u join history h on u.id_history = h.id h.type = 85 ) h2 on h1.id_user = h2.id_user h1.date_entered < h2.date_entered;
here's sql fiddle.
Comments
Post a Comment