PHP的PgSql:关于安排的问题

时间:2012-03-23 08:46:34

标签: php database postgresql select database-management

假设我有一张这样的表


      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|

每次用户注册新行时都会插入相应的值。


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

那么..我可以使用php安排行,它们首先按列'a'排列,然后按列'b'排列,然后排列'c'等,按降序排列?


      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|

因此,当我想选择具有最接近值的行时,我可以选择最近的行。

我刚接触数据库,如果有人可以帮助我,我将非常感激。谢谢。

1 个答案:

答案 0 :(得分:0)

要按照您要求的顺序获得结果

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

请注意,您要求按降序排序 a ,但您的示例显示相反,因为似乎是负面的......

如果您确实需要降序,即使值为负,您也可以尝试使用ABS函数。

SELECT * FROM your_table
ORDER BY ABS(a) DESC, b DESC, c DESC, d DESC, ABS(e) DESC