我在表中有两个整数列,其中第一列填充了一些随机数,第二列是空的。现在是否可以按升序对第一列进行排序,同时按降序对其进行排序并显示为第二列?我有下表的例子。
初始表:
col1 col2
5
7
3
9
2
输出:
col1 col2
2 9
3 7
5 5
7 3
9 2
答案 0 :(得分:1)
试试这个:
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