mysql排序问题

0pizxfdo  于 2021-06-21  发布在  Mysql
关注(0)|答案(1)|浏览(325)

需要最有效的方法来根据日期和状态订购一些项目/活动的想法。
目前我有以下疑问。

select tevent.ID, tevent.ename, e_dates.edate, tevent.status, tevent.eactive  from tevent 
LEFT JOIN e_dates on tevent.ID=e_dates.EID) Where tevent.status <> 'delete' and tevent.eactive ='Y'  group by tevent.id order 
by (case when tevent.status = 'closed' and e_dates.edate >= curdate() then 0 
else 1 end), edates.edate desc

我用了一个案例,但它排序的状态,然后按日期,这混淆了人们。
下面是输出

+----+--------------+-----+-----------+
| ID |Edate           | Ename   | Status  | 
+----+--------------+-----+-----------+
|  2  | 2018-09-21    | Event2  | Closed  |
|  5  | 2018-09-15    | Event5  | Closed  | 
|  3  | 2018-12-12    | Event3  | Open    |
|  6  | 2018-10-25    | Event6  | Approved|
|  4  | 2018-10-25    | Event4  | Open    |
|  7  | 2018-10-15    | Event7  | Pending |
|  10 | 2018-10-01    | Event10 | Open    |
|  1  | 2018-09-30    | Event1  | Open    |
|  4  | 2018-09-30    | Event4  | Open    |
|  8  | 2018-09-01    | Event8  | Closed  |
|  11 | 2018-08-25    | Event11 | Closed  |
+----+--------------+-----+-----------+

编辑:我试图完成的是列出事件日期早于事件日期的已结束事件。ie(id 2和id 5)
但希望asc按时间顺序列出未结、已批准、待定的事件状态。
以及关闭事件,其中事件日期(edate)在底部列出的当前日期(now())之后
例如:

+----+--------------+-----+-----------+
    | ID |Edate           | Ename   | Status  | 
    +----+--------------+-----+-----------+
    |  2  | 2018-09-21    | Event2  | Closed  |
    |  5  | 2018-09-15    | Event5  | Closed  | 
    |  1  | 2018-09-30    | Event1  | Open    |*
    |  4  | 2018-09-30    | Event4  | Open    |*
    |  10 | 2018-10-01    | Event10 | Open    |*
    |  7  | 2018-10-15    | Event7  | Pending |*
    |  4  | 2018-10-25    | Event4  | Open    |*
    |  6  | 2018-10-25    | Event6  | Approved|*
    |  3  | 2018-12-12    | Event3  | Open    |*
    |  11 | 2018-08-25    | Event11 | Closed  |    
    |  8  | 2018-09-01    | Event8  | Closed  |
    +----+--------------+-----+-----------+
zrfyljdw

zrfyljdw1#

我认为你几乎是对的。你需要在你的情况下陈述三个选项- 0的所有关闭的顶部,2的所有封闭的底部,和1的其他一切在中间。然后删除日期上的desc,使日期在每个部分中升序,这样您的orderby子句应该如下所示

order by (
    case when tevent.status = 'closed' and e_dates.edate >= curdate() then 0 
    when tevent.status = 'closed' and e_dates.edate < curdate() then 2
    else 1 end
), edates.edate

或者你可以写同样的东西

order by (
    case when tevent.status = 'closed' 
        then case when e_dates.edate >= curdate() then 0 else 2 end
    else 1 end
), edates.edate

相关问题