mysql使用日期和状态字段的订购问题

时间:2018-09-13 15:11:04

标签: mysql sql-order-by

需要有关根据日期和状态订购某些项目/事件的最有效方法的想法。

当前我有以下查询。

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按时间顺序列出OPEN,APPROVED,PENDING的事件状态。

关闭事件,事件日期(日期)晚于底部列出的当前日期(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  |
    +----+--------------+-----+-----------+

0 个答案:

没有答案