选择顺序记录

时间:2011-02-15 09:03:51

标签: mysql

CREATE TABLE `sequence` (`id` int(11) NOT NULL auto_increment, `load_status` varchar(100) default NULL, PRIMARY KEY  (`id`));
INSERT INTO `sequence` VALUES (1,'success'),(2,'success'),(3,'success'),(4,'fail'),(5,'success');

mysql>select * from sequence;
+----+-------------+
| id | load_status |
+----+-------------+
|  1 | success     | 
|  2 | success     | 
|  3 | success     | 
|  4 | fail        | 
|  5 | success     | 
+----+-------------+
5 rows in set (0.00 sec)

我想要按顺序显示“成功”的所有状态。将返回ID 1,2和3。但除非获得另一次成功,否则不会选择ID 5。

4 个答案:

答案 0 :(得分:1)

如果您正在处理大量数据,这种方法可能不可行(我对大型数据库缺乏经验),但这可能有所帮助。

我们的想法是创建子查询以获取相邻的行:

mysql> select id, load_status, (select load_status from test as t2 where t2.id < t1.id order by t2.id desc limit 1) as prev_load_status, (select load_status from test as t3 where t3.id > t1.id order by t3.id asc limit 1) as next_load_status from test as t1;
+----+-------------+------------------+------------------+
| id | load_status | prev_load_status | next_load_status |
+----+-------------+------------------+------------------+
|  1 | success     | NULL             | success          |
|  2 | success     | success          | success          |
|  3 | success     | success          | fail             |
|  4 | fail        | success          | success          |
|  5 | success     | fail             | NULL             |
+----+-------------+------------------+------------------+
5 rows in set (0.00 sec)

我最初在那里抛出一个HAVING子句,将结果限制在集群中:

mysql> select id, load_status, (select load_status from test as t2 where t2.id < t1.id order by t2.id desc limit 1) as prev_load_status, (select load_status from test as t3 where t3.id > t1.id order by t3.id asc limit 1) as next_load_status from test as t1 having load_status = prev_load_status or load_status = next_load_status;
+----+-------------+------------------+------------------+
| id | load_status | prev_load_status | next_load_status |
+----+-------------+------------------+------------------+
|  1 | success     | NULL             | success          |
|  2 | success     | success          | success          |
|  3 | success     | success          | fail             |
+----+-------------+------------------+------------------+
3 rows in set (0.00 sec)

但事实证明,您可以将子查询放在WHERE子句中:

mysql> select id, load_status from test as t1 where load_status = (select load_status from test as t2 where t2.id < t1.id order by t2.id desc limit 1) or load_status = (select load_status from test as t3 where t3.id > t1.id order by t3.id asc limit 1);
+----+-------------+
| id | load_status |
+----+-------------+
|  1 | success     |
|  2 | success     |
|  3 | success     |
+----+-------------+
3 rows in set (0.00 sec)

答案 1 :(得分:0)

试试这个

SELECT * FROM序列 ORDER BY FIELD(load_status,'Success','fail');

答案 2 :(得分:0)

有些事情让我对这个解决方案的速度感到烦恼,但我想也许你可以使用它:

SELECT current.id 
FROM sequence AS current 
LEFT JOIN sequence AS next ON(current.id = next.id+1)
WHERE (current.load_status = 'success' AND next.load_status = 'success') OR (current.load_status = 'success' AND next.load_status = 'fail') OR current.load_status = 'success';

答案 3 :(得分:0)

好的,我尝试了一些针对不同数据集显示出良好结果的解决方案,但始终存在一些问题。不知道为什么我认为不需要检查前一行......最后我能想到的是:

SELECT current.id
FROM sequence AS current 
LEFT JOIN sequence AS next ON(next.id = (SELECT id FROM sequence WHERE id > current.id ORDER BY id DESC LIMIT 1))
LEFT JOIN sequence AS prev ON(prev.id = (SELECT id FROM sequence WHERE id < current.id ORDER BY id DESC LIMIT 1))
WHERE (current.load_status = 'success' AND next.load_status = 'success') OR (current.load_status = 'success' AND next.load_status = 'fail') OR (current.load_status = 'success' AND prev.load_status = 'success')
ORDER BY id

希望它最终解决问题