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 请问这个是什么意思?
  • ¥15 STM32驱动继电器
  • ¥15 Windows server update services
  • ¥15 关于#c语言#的问题:我现在在做一个墨水屏设计,2.9英寸的小屏怎么换4.2英寸大屏
  • ¥15 模糊pid与pid仿真结果几乎一样
  • ¥15 java的GUI的运用
  • ¥15 我想付费需要AKM公司DSP开发资料及相关开发。
  • ¥15 怎么配置广告联盟瀑布流
  • ¥15 Rstudio 保存代码闪退
  • ¥20 win系统的PYQT程序生成的数据如何放入云服务器阿里云window版?