php - mysql query to get the min values according to the latesttime stamp -
hi have try find solution query minvalues along latest time stamp.
i have following tables
table1
sno user_id name subject rank timestamp 1 10 ss maths1 1 2014-12-05 17:24:33 2 10 ss maths2 2 2014-12-05 17:24:33 3 10 ph phy1 3 2014-12-05 17:24:33 4 10 ph phy2 4 2014-12-05 17:24:33 5 10 ss maths1 2 2014-12-04 17:24:33 6 10 ss maths2 1 2014-12-04 17:24:33 7 10 ph phy1 3 2014-12-04 17:24:33 8 10 ph phy2 4 2014-12-04 17:24:33
tabe12
sno name status 1 ss active 2 ph active 3 inactive
so trying following result
sno userid name subject rank timpestamp 1 10 ss maths1 1 2014-12-05 17:24:33 2 10 ph phy1 3 2014-12-05 17:24:33
so far tried query able latest time , least rank values subjects distinct name. not connected each other means getting latesttime stamp , least value other row. please me how solve it.
select distinct(t1.`name`), t1.`user_id`, t1.`subject`, max(t1.`timestamp`) latest_timestamp, min(t1.`rank `) rank table1 t1,table2 t2 t1.`user_id`='10' , t2.`status` = 'active' , t1.`name` = t2.`name` group t1.`exercise_id` order t1.`quality_id`, t1.`timestamp` asc
relational databases work on sets. think of data in different sets
- you need set combines t1 , t2
- you need subset of t1 consists of min rank , max timestamp each user_id , name.
so t1 , t2 joined generating universe of data , columns need. join subquery contianing subset filter out non max timestamp , non-min rank want.
select t1values.name, t1values.user_id, t1values.subject, t1values.rank, t1values.timestamp table2 t2 inner join table1 t1values on t1values.sno = t2.sno , t2.status='active' inner join (select user_id, name max(timestamp) maxtime, min(rank) mrank table1 group user_id, name) t1 on t1.name = t1values.name , t1.user_id = t1values.user_id , t1.maxtime = t1values.timestamp , t1.rank = t1values.mrank t1values.user_id='10' order t1values.quality_id, t1values.timestamp asc
Comments
Post a Comment