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 oracle集群安装出bug
  • ¥15 关于#python#的问题:自动化测试
  • ¥20 问题请教!vue项目关于Nginx配置nonce安全策略的问题
  • ¥15 教务系统账号被盗号如何追溯设备
  • ¥20 delta降尺度方法,未来数据怎么降尺度
  • ¥15 c# 使用NPOI快速将datatable数据导入excel中指定sheet,要求快速高效
  • ¥15 再不同版本的系统上,TCP传输速度不一致
  • ¥15 高德地图2.0 版本点聚合中Marker的位置无法实时更新,如何解决呢?
  • ¥15 DIFY API Endpoint 问题。
  • ¥20 sub地址DHCP问题