Pergunta

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.

Foi útil?

Solução

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
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top