鉴于此表:
+----+-----------+--------+
| id | condition | values |
+----+-----------+--------+
| 1 | a | 1 |
+----+-----------+--------+
| 2 | a | 2 |
+----+-----------+--------+
| 3 | a | 3 |
+----+-----------+--------+
| 4 | a | 4 |
+----+-----------+--------+
| 5 | b | 5 |
+----+-----------+--------+
| 6 | b | 6 |
+----+-----------+--------+
如何获得一个以id=3
开头的新表(包括),直到condition = b
(不包括):
+----+-----------+--------+
| id | condition | values |
+----+-----------+--------+
| 3 | a | 3 |
+----+-----------+--------+
| 4 | a | 4 |
+----+-----------+--------+
添加了小提琴: http://sqlfiddle.com/#!2/9882f7
基本上我想要一个匹配的第一个条件(通过特定的列 - id)和第二个(通过不同的列 - 条件)之间的表
答案 0 :(得分:2)
您需要停止将SQL数据视为有任何订单。考虑集合中的SQL数据;您必须按值搜索,而不是按位置搜索。
SELECT t1.*
FROM t AS t1
JOIN (
SELECT MIN(id) AS id FROM t
WHERE id >= 3 AND `condition` = 'b'
) AS t2
WHERE t1.id >= 3 AND t1.id < t2.id
ORDER BY t1.id
答案 1 :(得分:0)
这样的事情:
select t.*
from table t
where id >= 3 and id < (select min(t2.id) from table t2 where t2.condition = 'b');
编辑:
此查询在SQL Fiddle上运行良好:
select t.*
from t
where id >= 3 and id < (select min(t2.id) from t t2 where t2.condition = 'b');
答案 2 :(得分:0)
如果我理解你的要求,我相信这对你有用:
SELECT id, condition, values
FROM tableName
WHERE id > 2
AND condition != b
ORDER BY id
我希望这适合你。