mysql - How do I select multiple maximum and minimums from a table -


i have written query has returned column of numbers , ordered lowest highest. need display highest , lowest value. planning on using simple select max(x),min(x) ... table contains multiple minimums , statement selects first minimum.

 example of table  name x     1  b    1  c    1  d    2  e    5 

how display

 name x     1  b    1  c    1  e    5 

this example, there range of rows between max , min.

you can join subquery return min , max. union remove duplicates if max = min.

sqlfiddledemo

create table tab(name nvarchar(100), x int);  insert tab values ('a',    1), ('b',    1), ('c',    1), ('d',    2), ('e',    5);  select t.* tab t  join (select min(x) val        tab       union       select max(x) val       tab) sub  on t.x = sub.val; 

Comments

Popular posts from this blog

php - Invalid Cofiguration - yii\base\InvalidConfigException - Yii2 -

How to show in django cms breadcrumbs full path? -

ruby on rails - npm error: tunneling socket could not be established, cause=connect ETIMEDOUT -