doudandui1592 2011-12-02 19:02
浏览 121
已采纳

mysql按视图限制但按字母顺序排序?

I have a table like that I want to take the 20 most hit singers and order them (these 20 singers) alphabeticly.

   id    name         hit
    ----------------
    1     Beyonce      2540
    2     Eminem       1432
    3     Pink         1642
    4     Shakira      1234
    .
    .

For example I use this code

$query = mysql_query("SELECT * FROM pm_categories ORDER BY hit DESC limit 20");

I take the 20 with the most hits, but I want to also order them alphabeticly. How can I do that?

  • 写回答

2条回答 默认 最新

  • dsgawmla208057 2011-12-02 19:15
    关注

    This should do it:

    SELECT *
    FROM pm_categories
    WHERE id IN (
      SELECT id
      FROM pm_categories
      ORDER BY hit DESC
      LIMIT 20
    ) ORDER BY name
    

    You need to query based on the hits separately, and then use the ids of the top 20 to query and sort by name.

    Since you have an older version of mysql, you will need to do a join instead.

    SELECT cat.*
    FROM (
      SELECT id
      FROM pm_categories
      ORDER BY hit DESC
      LIMIT 20
    ) top_hits
    JOIN pm_categories cat ON top_hits.id = cat.id
    ORDER BY cat.name
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题
  • ¥15 Visual Studio问题
  • ¥20 求一个html代码,有偿
  • ¥100 关于使用MATLAB中copularnd函数的问题