dou5454954610 2013-09-03 11:31
浏览 33
已采纳

PHP按问题排序

I am having hard time to solve this puzzle. I have a website which is showing some products. I am trying to show the product by ASC order but it is not working properly.

Here is my current query and output:

$sql=executeQuery("select * from ".PREFIX."prod_info where prodId='".$pId."' order by ProductName+0) ASC");


AM 65
AM 67
AM 69
AM 104 (Chuck) 
AM 105 (Sin) 
AM 73
AM 76
AC 20 

Any help would be appreciated

  • 写回答

1条回答 默认 最新

  • dtx6087 2013-09-03 11:39
    关注

    You should have:

    order by ProductName
    

    Instead of:

    order by ProductName+0
    

    The latter converts ProductName to an integer. If it starts with a letter, then it will be treated as 0. What is probably happening is that all are starting with letters, all get ordered by the value 0 -- and nothing interesting is happening.

    EDIT:

    You appear to want to order the ProductName alphabetically by the first two digits and then numerically by the rest. You can do this as:

    order by left(ProductName, 2),
             char_length(ProductName),
             ProductName
    

    If you really have extraneous words at the end, after the number, then it gets harder. The following may work better:

    order by left(ProductName, 2),
             substr(ProductName, 3) + 0,
             ProductName
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥35 平滑拟合曲线该如何生成
  • ¥100 c语言,请帮蒟蒻写一个题的范例作参考
  • ¥15 名为“Product”的列已属于此 DataTable
  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集