简体   繁体   中英

how to get a sorted result in mysql query?

I am trying to get a sorted result from a MySQL query and i dont have any idea of making the query.

I have a example Mysql table as follows :

     id | cat_type
     1  |  free
     2  |  free
     3  |  free
     4  |  free
     5  |  paid
     6  |  paid
    ... |  ...
    ... |  ...
    ... |  ...
   and it goes on.

The result should be as follows from the above table :

     id | cat_type
     1  | free 
     2  | free 
     5  | paid

     3  | free 
     4  | free 
     6  | paid

    ... | free
    ... | free
    ... | paid
  and goes on(should be sorted)

The total result should be splitted as 3. In the first three result, first two should be free and the next one should be paid likewise all the remaining result should be sorted as two free and one paid .

Any idea how to accomplish this using a query?

NOTE : The most important is that it should be sorted as two free and one paid. Not by ID.

Thanks.

For you particular data, you could do:

order by (case when cat_type = 'free' then id*1.0
               else 2*id - 7.5
          end)

This seems very arcane. If you have a more general problem, you should ask another question, giving a better description of what you really want to accomplish.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM