dsv768456 2016-11-21 16:32
浏览 69
已采纳

如何使用PHP mysql查询对数组值进行排序

My DB Table field Contain value with array of data I need to sort the first value alone

Pid | Price
  1 | 213,4566,112
  2 | 100,452,567
  3 | 653,344,6322
  4 | 55,222,42,44
  5 | 522

I want it to be sorted with price first value in ascending order like below

Pid | Price    
  4 | 55,222,42,44
  2 | 100,452,567
  1 | 213,4566,112
  5 | 522
  3 | 653,344,6322

I need a Mysql query using PHP.

  • 写回答

1条回答 默认 最新

  • dongxinyue2817 2016-11-21 16:43
    关注

    You can use the following query using SUBSTRING_INDEX:

    SELECT * FROM table 
    ORDER BY CAST(SUBSTRING_INDEX(Price, ',', 1) AS SIGNED) ASC
    

    Demo: http://sqlfiddle.com/#!9/3f7b7/2

    Hint: You should avoid a column like Price with comma seperated values.
    Normalize your database / tables!

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?