mysql - Get max and min values along with all values and multiple where clause -


i have query works good

  select cm.id ,cm.edited,cm.date_edited,cm.voteup,cm.votedown    chat_messages cm    timestampdiff(second,cm.date_edited,'$now') < 10 group cm.id 

which gives me entries edited in less 10 seconds.

but along im trying max(voteup) , min(votedown)

but dont affect first entries of first query . how can combine entries need ?

example:

if im getting 3 newly updates entries . want them 3 plus max of voteup , votedown .

example:

    id   edited  date_edited          voteup    votedown     37      0      2016-03-05 22:13:03    5         0     38      0      2016-04-02 11:15:00    3         7     39      0      2016-03-05 22:10:06    10        6     40      0      2016-03-20 21:40:06    5         0     41      1      2016-04-20 22:28:59    5         0     42      1      2016-03-20 21:59:15    0         20     43      1      2016-04-21 22:20:25    8         0     <---- new updated 

my wished result

    id   edited  date_edited         voteup  votedown  maxup  maxdown     39      0      2016-03-05 22:10:06    10        6    10     null     42      1      2016-03-20 21:59:15    0         20   null   20     43      1      2016-04-21 22:20:25    8         0    null   null   

my $now time 2016-04-21 22:20:20

explanation:

   -id 39 having maxup vote want     -id 42  having maxdown want     -id 43 newly updated in period of 10 seconds. 

so general want new updated entries pls max , down .

if many max voteup values same choose 1 have min votedown

any solution pls ?

here my sqlfiddle example

edit: oh sorry meant id . wish question clear enter image description here

you want use union statement:

select * (     select cm.id ,cm.edited,cm.date_edited,cm.voteup,cm.votedown          , voteup maxup, null maxdown     chat_messages cm     order voteup desc, votedown     limit 1 ) union select * (     select cm.id ,cm.edited,cm.date_edited,cm.voteup,cm.votedown          , null maxup, votedown maxdown     chat_messages cm     order votedown desc, voteup     limit 1 ) b union select * (   select cm.id ,cm.edited,cm.date_edited,cm.voteup,cm.votedown          , null maxup, null maxdown   chat_messages cm   timestampdiff(second,cm.date_edited,'2016-04-21 22:20:20') < 10 ) c 

note used '2016-04-21 22:20:20', want substitute $now in


Comments

Popular posts from this blog

Django REST Framework perform_create: You cannot call `.save()` after accessing `serializer.data` -

Why does Go error when trying to marshal this JSON? -