【问题标题】:sql statement order by with condition always failed?sql语句order by with条件总是失败?
【发布时间】:2016-06-18 01:12:15
【问题描述】:

我想显示具有自定义顺序的所有行,部分第一行与 2 天前到当前日期具有相同的数据(使用 2016-06-18 作为当前日期),之后请自定义基于最小数量的 dist 的行, 我使用这个 sql 来检索数据,但总是失败。

SELECT pwaktuserver as date,
            acos(cos(-7.47353794753 * (PI()/180)) *
            cos(112.343534533 * (PI()/180)) *
            cos(plat * (PI()/180)) *
            cos(plong * (PI()/180))
            +
            cos(-7.47353794753 * (PI()/180)) *
            sin(112.343534533 * (PI()/180)) *
            cos(plat * (PI()/180)) *
            sin(plong * (PI()/180))
            +
            sin(-7.47353794753 * (PI()/180)) *
            sin(plat * (PI()/180))
            ) * 3959 as Dist
                    FROM tbpos INNER JOIN tbanggota ON tbanggota.id_a = tbpos.id_a WHERE stsTampil <> '2'
                    GROUP BY tbpos.posid
                    ORDER BY case when date(pwaktuserver)>=curdate()-2 then Dist*(-1) else Dist end

上面sql语句的结果

注意:1. 黄色框是包含 2 天前的日期的行组

  1. 蓝色框是一组行,由距离和行排序 asc 组成。

sort of data return corect value 黄色框作为第一部分,蓝色框作为第二部分,但我的问题是黄色框中的日期不是 ordered DESC,我想让黄色框有序 desc,如何我实现了我的目标?谢谢

【问题讨论】:

    标签: mysql sql


    【解决方案1】:

    只需添加另一个条件。

      ORDER BY CASE WHEN date(pwaktuserver)>=curdate()-2 THEN date(pwaktuserver)
                                                         ELSE null
               END DESC,  
               CASE WHEN date(pwaktuserver)>=curdate()-2 THEN Dist*(-1) 
                                                         ELSE Dist 
               END ASC -- OR DESC
    

    【讨论】:

    • Booooomm...为什么我现在不问昨天,谢谢你的回答,我昨天的堆栈。你是火箭人......
    猜你喜欢
    • 2014-06-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-13
    • 1970-01-01
    • 2021-12-23
    • 1970-01-01
    • 2020-10-26
    相关资源
    最近更新 更多