douping1993 2013-11-16 08:42
浏览 27
已采纳

按PHP排序特别回声

Ok i have this in my database as example

id |  x | y  | z  | status | statusb
 1 | 20 | 12 | 13 |    1   |    2
 2 | 40 | 32 | -- |    3   |    2
 3 | 50 | 31 | 15 |    2   |    1

im doing a select like

"select * FROM table ORDER BY GREATEST(COALESCE(x, 0),COALESCE(y, 0), COALESCE(z, 0)) DESC"

And the echo inside a for each.. is like

if (status==1) { echo $row_x style1}
if (status==2) { echo $row_x style2}
if (status==3) { echo $row_x style3}
if (statusb==1) { echo $row_y style4}
if (statusb==2) { echo $row_y style5}

I get something like

 id3 - x50 style2
 id3 - y31 style4
 id2 - x40 style3
 id2 - y32 style5
 id1 - x20 style1
 id1 - y12 style5

What i need is

 id3 - x50 style2
 id2 - x40 style3
 id2 - y32 style5
 id3 - y31 style4
 id1 - x20 style1
 id1 - y12 style5

Thanks to Chinnu R for the fast answer the problem is that y or x can have 0 (not null) as default when the col is created the value is 0 unless the user update that..

So my problem is 0 affecting the query using the answer

ORDER BY x DESC, y DESC, z desc

id |  x | y  | z  | status | statusb
 1 | 20 | 12 | 13 |    1   |    2
 2 | 40 | 0  | -- |    3   |    2
 3 | 50 | 31 | 15 |    2   |    1
  • 写回答

1条回答 默认 最新

  • dtdr84101 2013-11-16 08:48
    关注

    Can you try this in SQL query,

    ORDER BY x DESC, y DESC, z DESC
    

    For not null WHERE y IS NOT NULL

    For not equal to zero, WHERE y !='0'

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

报告相同问题?

悬赏问题

  • ¥15 ue5 .3之前好好的现在只要是激活关卡就会崩溃
  • ¥50 MATLAB实现圆柱体容器内球形颗粒堆积
  • ¥15 python如何将动态的多个子列表,拼接后进行集合的交集
  • ¥20 vitis-ai量化基于pytorch框架下的yolov5模型
  • ¥15 如何实现H5在QQ平台上的二次分享卡片效果?
  • ¥15 python爬取bilibili校园招聘网站
  • ¥30 求解达问题(有红包)
  • ¥15 请解包一个pak文件
  • ¥15 不同系统编译兼容问题
  • ¥100 三相直流充电模块对数字电源芯片在物理上它必须具备哪些功能和性能?