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 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?