[英]mysql select query with where condition in a range of records
我只想从满足条件的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 |
现在,我只想获取表中行10-20之间已completed=2
那些记录。
我尝试了类似的东西:
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条件。
这样的事情怎么样:(它在我测试的版本(mySQL 5.6上似乎很好)):
select * from (
select * from table1 limit 10, 10) t1 where t1.completed = 2
在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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.