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'

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

报告相同问题?

悬赏问题

  • ¥170 如图所示配置eNSP
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改
  • ¥20 wireshark抓不到vlan
  • ¥20 关于#stm32#的问题:需要指导自动酸碱滴定仪的原理图程序代码及仿真
  • ¥20 设计一款异域新娘的视频相亲软件需要哪些技术支持
  • ¥15 stata安慰剂检验作图但是真实值不出现在图上
  • ¥15 c程序不知道为什么得不到结果
  • ¥15 键盘指令混乱情况下的启动盘系统重装