-1

我有 2 条选择语句,一条用于获取最新的失败状态,一条用于获取最新的错过状态。在第三个中,如果最新状态是错过或失败但它似乎已经合并了数据,我需要有一个声明?我使用 LIKE 因为错过和失败可能会略有不同

mysql> select max(scheduled_start),node_name,schedule_name,status from `events_server` WHERE status LIKE '%Failed%'and node_name='name';
+----------------------+----------------+---------------+-----------+
| max(scheduled_start) | node_name      | schedule_name | status    |
+----------------------+----------------+---------------+-----------+
| 2013-01-30 15:00:00  | name           | name_1500     | Failed  1 |
+----------------------+----------------+---------------+-----------+
1 row in set (0.01 sec)

mysql> select max(scheduled_start),node_name,schedule_name,status from `events_server` WHERE status LIKE '%Missed%'and node_name='name';
+----------------------+----------------+---------------+--------+
| max(scheduled_start) | node_name      | schedule_name | status |
+----------------------+----------------+---------------+--------+
| 2013-01-23 08:00:00  | name           | NAME_0800     | Missed |
+----------------------+----------------+---------------+--------+
1 row in set (0.01 sec)

mysql> select max(scheduled_start),node_name,schedule_name,status from `events_server` WHERE (status LIKE '%Failed%' OR status LIKE '%Missed%') and node_na
me='name';
+----------------------+----------------+---------------+--------+
| max(scheduled_start) | node_name      | schedule_name | status |
+----------------------+----------------+---------------+--------+
| 2013-01-30 15:00:00  | name           | NAME_0800     | Missed |
+----------------------+----------------+---------------+--------+
1 row in set (0.01 sec)
4

1 回答 1

1

您是否尝试过类似于此的方法,它将旋转数据并为每个状态返回一个单独的列。这将为您max(scheduled_start)提供FailedMissed状态。但是您还需要添加 aGROUP BY才能得到结果:

select 
    max(case when status LIKE '%Failed%' then scheduled_start end) Failed,
    max(case when status LIKE '%Missed%' then scheduled_start end) Missed,
    node_name,
    schedule_name
from `events_server`
where node_name='name'
 AND (status LIKE '%Missed%' OR status LIKE '%Failed%')
group by node_name, schedule_name
于 2013-01-30T16:53:00.913 回答