mysql - How to select two fields of different rows? -


i have table structure:

id int min int max int 

i want order ascendently , select max of first row, , min of second one.

so did query, , before values need:

select min, max mytable order min asc limit 2; 

also tried this:

select cm_max mytable order cm_min limit 1 union select cm_min mytable order cm_min limit 1,1; 

but not work... there way select fields i'll use?

if want values returned in 1 row can do

select min(case when rnum = 1 cm_max end) cm_max,        min(case when rnum = 2 cm_min end) cm_min   (   select id, cm_min, cm_max, @n := @n + 1 rnum     medidas, (select @n := 0) n    order cm_min    limit 2 ) q 

what gets 2 records order condition , assigns row number each row in inner select. in outer select pivot values using case , row numbers.

or

select q1.cm_max, q2.cm_min   (   select id, cm_min, cm_max     medidas    order cm_min    limit 1 ) q1 cross join (   select id, cm_min, cm_max     medidas    order cm_min    limit 1, 1 ) q2  

in query grab 2 records of interest in sub queries , use cross join join 2 records , output needed values.

here sqlfiddle demo both queries


Comments

Popular posts from this blog

php - Calling a template part from a post -

Firefox SVG shape not printing when it has stroke -

How to mention the localhost in android -