dpus81500574 2013-04-06 09:35
浏览 47
已采纳

PHP中mysql中同一行中的前5个数字

How to get the first highest numbers in the same row in mysql for example i have this row

I have this database

Name Level1 Level2 Level3 Level4 Level5 Level6 Level7 Level8 Level9 Level10 Level11 Level12
armar 80 50 30 60 80 60 90 35 90 66 55 22

ok i want to get the top 5 levels in that row in mysql and order from higher to lower

example result will be : 90 90 80 80 60

Thanks!

  • 写回答

2条回答 默认 最新

  • douxing1353 2013-04-06 09:46
    关注

    A PHP solution would be better here, but if you just need a MySQL query you could use this:

    SELECT
      Name,
      SUBSTRING_INDEX(SUBSTRING_INDEX(Levels, ',', 1), ',', -1) Top1,
      SUBSTRING_INDEX(SUBSTRING_INDEX(Levels, ',', 2), ',', -1) Top2,
      SUBSTRING_INDEX(SUBSTRING_INDEX(Levels, ',', 3), ',', -1) Top3,
      SUBSTRING_INDEX(SUBSTRING_INDEX(Levels, ',', 4), ',', -1) Top4,
      SUBSTRING_INDEX(SUBSTRING_INDEX(Levels, ',', 5), ',', -1) Top5
    FROM (
      SELECT Name, GROUP_CONCAT(Level ORDER BY Level DESC) Levels
      FROM (
        SELECT Name, Level1 Level FROM yourtable
        UNION ALL
        SELECT Name, Level2 FROM yourtable
        UNION ALL
        SELECT Name, Level3 FROM yourtable
        UNION ALL
        SELECT Name, Level4 FROM yourtable
        UNION ALL
        SELECT Name, Level5 FROM yourtable
        UNION ALL
        SELECT Name, Level6 FROM yourtable
        UNION ALL
        SELECT Name, Level7 FROM yourtable
        UNION ALL
        SELECT Name, Level8 FROM yourtable
        UNION ALL
        SELECT Name, Level9 FROM yourtable
        UNION ALL
        SELECT Name, Level10 FROM yourtable
        UNION ALL
        SELECT Name, Level11 FROM yourtable
        UNION ALL
        SELECT Name, Level12 FROM yourtable
      ) s
      GROUP BY Name
    ) s
    

    Please see fiddle here.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥30 Unity接入微信SDK 无法开启摄像头
  • ¥20 有偿 写代码 要用特定的软件anaconda 里的jvpyter 用python3写
  • ¥20 cad图纸,chx-3六轴码垛机器人
  • ¥15 移动摄像头专网需要解vlan
  • ¥20 access多表提取相同字段数据并合并
  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算
  • ¥15 powerbuilder中的datawindow数据整合到新的DataWindow
  • ¥20 有人知道这种图怎么画吗?
  • ¥15 pyqt6如何引用qrc文件加载里面的的资源