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

如何使用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.

图片转代码服务由CSDN问答提供 功能建议

我的数据库表字段包含我需要单独排序第一个值所需的数据数组的值

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

我希望它按价格优先顺序排序,如下所示

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

我需要使用PHP进行Mysql查询。

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

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!

    已采纳该答案
    打赏 评论

相关推荐 更多相似问题