mysql - Sql 语句 order by with 条件?

标签 mysql sql

此 myTable 显示 order by no asc 的所有行,

enter image description here

我的问题是,我想显示带有自定义 order by 的所有行, 部分第一行与当前日期具有相同的数据(使用 2015-09-18 作为当前日期),之后请根据最小的数字自定义行,因此之后的结果自定义 order by 将从 5,4,1,2,3 排序。

我如何用sql语句实现这一点?谢谢。

最佳答案

SELECT *
FROM table
ORDER BY 
    CASE
        WHEN date(date) = curdate() THEN no * (-1) -- when date is current date, sort descending (ascending by negative ID, to reverse order)
        ELSE no -- else keep default ascending sort order
    END

关于mysql - Sql 语句 order by with 条件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32648172/

相关文章:

c# - mysqldump.exe 需要密码,即使它在参数中给出

mysql - SQL集划分

SQL计算字符串的一部分

mysql - 为什么给定的语法在 mysql 中有效?

sql - 将 NOT IN 转换为 NOT EXISTS

mysql - 使用选择查询问题

MySQL 的联合和排序通过帮助

mysql - 使用 MySQL 将 Symfony 应用程序部署到 AWS

mysql - SQL 前向引用语法

mysql - 如何优化具有类似子查询的Mysql查询?