根据标准按不同领域排序

时间:2016-10-27 11:43:51

标签: mysql sql

我有两个系列的帖子。每个帖子都有一定比例的进度( // Creates the ArrayList ArrayList<Integer> numberList = new ArrayList<Integer>(); // Loop creates an array of numbers starting at "from" ending at "to" for (int i = from; i < to + 1; i++) { numberList.add(i); } // Check the array to see whether number contains digit // Code checks whether x contains 5, n == one digit // IMPORTANT: Doesn't work on the first half of numbers i.e / will remove 3 but not 30 for (int j = 0; j < numberList.size(); j++) { int number = (int) numberList.get(j); // This can be any integer int thisNumber = number >= 0 ? number: -number; // if statement in case argument is negative int thisDigit; while (thisNumber != 0) { thisDigit = thisNumber % 10; // Always equal to the last digit of thisNumber thisNumber = thisNumber / 10; // Always equal to thisNumber with the last digit chopped off, or 0 if thisNumber is less than 10 if (thisDigit == omittedDigit) { numberList.remove(j); j--; } } } // Return the completed Array list return numberList; } )和结束时间(project_percent)。我希望以后首先显示end_date的帖子(&#39; DESC&#39;)然后我想用过去的订单按百分比显示项目end_date。如何只使用一个查询来完成此操作?

(如果日期不是通过百分比不重要,只有日期很重要,但如果日期过去我想按百分比排序)

end_date

select * from posts ...

*今天是-------------------------------------- | post | end_date | project_percent| | P1 | 28-10-2016| 50 | | P2 | 26-10-2016| 40 | | P2 | 30-10-2016| 20 | | P3 | 24-10-2016| 75 |

我希望输出如下:

27-10-2016

*前两个按日期排序,后两个按persent排序

1 个答案:

答案 0 :(得分:1)

尝试一下:

select *, end_date < now() as flag from posts
order by flag
, case when flag = 0 then end_date end desc
, case when flag = 1 then project_percent end desc;

使用

可以使其更简单
select * from posts
order by 
    case when end_date < now() = 0 then end_date end desc
    , case when end_date < now() = 1 then project_percent end desc;

但是第一个版本更适合调试。