我想只选择条件满足的10条记录的特定范围内的记录。该范围可以是0-10或20-30或35-45或任何范围。
select Cid, Company_Name, completed from table1;
+-------+----------------------------------------------------+-----------+
| Cid | Company_Name | completed |
+-------+----------------------------------------------------+-----------+
| 73855 | company 1 | 1 |
| 73894 | company 2 | 2 |
| 73906 | company 3 | 2 |
| 73909 | company 4 | 2 |
| 73921 | company 5 | 1 |
| 73924 | company 6 | 1 |
| 73936 | company 7 | 2 |
| 73939 | company 8 | 2 |
| 73955 | company 9 | 2 |
| 73994 | company 10 | 2 |
| 74003 | company 11 | 0 |
现在我想只获取表格中包含completed=2
的第10-20行之间的记录。
我尝试过类似的事情:
select Cid, Company_Name, completed from table1 t1 where completed=2 in (select Cid, Company_Name, completed from table1 t2 limit 10,10);
但是这给了我错误:
ERROR 1235 (42000): This version of MySQL doesn't yet support 'LIMIT & IN/ALL/ANY/SOME subquery'
使用这样的限制:
select Cid, Company_Name from table1 t1 where completed=2 limit 10,10;
首先应用条件,然后应用所选结果的限制。我想首先应用限制然后应用where条件。
答案 0 :(得分:0)
这样的事情:(它似乎在我测试的版本上工作得很好,这是mySQL 5.6):
select * from (
select * from table1 limit 10, 10) t1 where t1.completed = 2
答案 1 :(得分:0)
在mysql中我们可以通过在“where”之前添加“Offset”来限制:
select Cid, Company_Name from table1 t1 limit 10 offset 0 where completed=2
更新: 资料来源:http://dev.mysql.com/doc/refman/5.5/en/limit-optimization.html