douwen9345 2012-03-23 08:46
浏览 33
已采纳

PHP的PgSql:关于安排的问题

Let's say I have a table like this


      a    |     b     |     c     |     d     |     e     |  id |  user_id     |     
---------------------------------------------------------------------------------
  -2.3213  | 9999232342| 0.2099321 |  0.113399 |   -1221   |  1  |43124123321321|
---------------------------------------------------------------------------------
  -3.3213  | 2231232342| 0.3099321 |  0.203799 |   -1231   |  2  |34224123321321|
---------------------------------------------------------------------------------
  -4.3293  | 1111232342| 0.2099321 |  0.203799 |   -1241   |  3  |98787612321321|
---------------------------------------------------------------------------------
  -1.1133  | 2231232342| 0.1099321 |  0.213399 |   -1231   |  4  |76534123321321|
--------------------------------------------------------------------------------
  -4.2469  | 2231232342| 0.6099321 |  0.203399 |   -1261   |  5  |55542333321321|
---------------------------------------------------------------------------------
  -1.1133  | 2231232342| 0.1099321 |  0.103599 |   -1271   |  6  |12454123321321|
---------------------------------------------------------------------------------
  -3.3213  | 2231232342| 0.5099321 |  0.203599 |   -1281   |  7  |23123323321321|

and every time a user signs up a new row will be inserted with respective values.


---------------------------------------------------------------------------------
  -9.1133  | 2231232342| 0.1099321 |  0.103599 |   -1181   |  8  |23123323321321|

So.. can I arrange the rows using php in a way that they are arranged by column 'a' first, then column 'b', then column 'c', etc, in a descending order?


      a    |     b     |     c     |     d     |     e     |  id |  user_id     | 
---------------------------------------------------------------------------------
  -9.1133  | 2231232342| 0.1099321 |  0.103599 |   -1181   |  8  |23123323321321|
---------------------------------------------------------------------------------
  -4.3293  | 1111232342| 0.2099321 |  0.203799 |   -1241   |  3  |98787612321321|
--------------------------------------------------------------------------------
  -4.2469  | 2231232342| 0.6099321 |  0.203399 |   -1261   |  5  |55542333321321|
---------------------------------------------------------------------------------
  -3.3213  | 2231232342| 0.5099321 |  0.203599 |   -1281   |  7  |23123323321321|
---------------------------------------------------------------------------------
  -3.3213  | 2231232342| 0.3099321 |  0.203799 |   -1231   |  2  |34224123321321|    
---------------------------------------------------------------------------------
  -2.3213  | 9999232342| 0.2099321 |  0.113399 |   -1221   |  1  |43124123321321|
---------------------------------------------------------------------------------
  -1.1133  | 2231232342| 0.1099321 |  0.213399 |   -1231   |  4  |76534123321321|
---------------------------------------------------------------------------------
  -1.1133  | 2231232342| 0.1099321 |  0.103599 |   -1271   |  6  |12454123321321|

And therefore when I want to select the rows with the closest values I can just select the nearest rows.

I'm new to working with database and I'll be very grateful if someone can help me. Thank you.

  • 写回答

1条回答 默认 最新

  • duanke6057 2012-03-23 08:50
    关注

    To get result in the order you ask you could use

    SELECT * FROM your_table
    ORDER BY a ASC, b DESC, c DESC, d DESC, e ASC
    

    Note that you asked to sort a in descending order, but your example show us the contrary because a seems to be negative...

    If you really need descending order even if a value is negative you could try to use ABS function.

    SELECT * FROM your_table
    ORDER BY ABS(a) DESC, b DESC, c DESC, d DESC, ABS(e) DESC
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器