There are buttons on the main page that process the same query, but with a different search term.

(select id, name from table where yx=1; select id, name from table where yx=2...) 

The results of the query are processed and displayed on a separate page. Tell me how can the resulting list be sorted?

  • 2
    use ORDER BY - zb '

1 answer 1

Add to the end of the query the ORDER BY construct:

 select id, name from table where yx=1 ORDER BY id ASC; 

ASC - sorts in ascending order DESC - sorts in descending order

You can also combine fields and methods for sorting them:

 select id, name from table where yx=1 ORDER BY id ASC, name DESC; 
  • Well it is clear. But I meant to sort the resulting list. Ie the request passed, the result was displayed, but the user decided to sort, for example, by date or price these results. - Chromegolf
  • jquery plugin, datatables for example - zb '
  • one
    @Chromegolf, also through ORDER BY and sort. For example, you create a form with selects, and depending on the selected select you make a request for output: select id, name from table where yx = 1 ORDER BY date DESC; select id, name from table where yx = 1 ORDER BY price DESC; Otherwise, if no selections are selected, make the selection that is necessary. - andreyqin
  • Just do not forget about security and pass the field to be sorted in the request. It is better to make select with id and on the server already with the help of swtich select the field by which you want to sort. - ice178