Vivek
Vivek

Reputation: 4636

MySQL query: help needed

I have two integer columns in a table where the first column is populated with some random numbers and the second column is empty. Now is it possible to sort the first column in ascending order and at the same time sort it in descending order and display as the second column? I have the example of the table below.

Initial Table:

col1   col2
 5
 7
 3
 9
 2

Output:

col1   col2
 2      9
 3      7
 5      5
 7      3
 9      2

Upvotes: 0

Views: 93

Answers (1)

Marcx
Marcx

Reputation: 6826

Try this:

SELECT tb1.col1,tb2.col2 FROM 
  (SELECT @rownum:=@rownum+1 as rank, id as col1 
    FROM your_table, (SELECT @rownum:=0) as r 
    ORDER BY id ASC) as tb1 
JOIN
  (SELECT @rownum2:=@rownum2+1 as rank, id as col2 
    FROM your_table, (SELECT @rownum2:=0) as r 
    ORDER BY id DESC) as tb2
ON tb1.rank = tb2.rank

Upvotes: 1

Related Questions