如何在mysql查询中得到排序的结果? [英] how to get a sorted result in mysql query?

查看:92
本文介绍了如何在mysql查询中得到排序的结果?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从MySQL查询中获取排序结果,但我不知道进行查询.

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

我有一个示例Mysql表,如下所示:

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.

结果应如下表所示:

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

     3  | free 
     4  | free 
     6  | paid

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

总结果应拆分为3.在前三个结果中,前两个应为free,下一个应为paid,同样,所有其余结果应分类为两个free和一个paid.

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?

注意:最重要的是,应将其分类为两个免费和一个付费.不是通过ID.

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

谢谢.

推荐答案

对于您的特定数据,您可以执行以下操作:

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.

这篇关于如何在mysql查询中得到排序的结果?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆