假设CUSTOMERS是详细信息表。上面的查询工作正常但如果我指定偏移而不是查询结束我得到错误。
表名是sms_view http://mobiledetect.net/
查询:
SELECT SMS FROM sms_view WHERE read = 2 LIMIT 5 OFFSET 0;
结果是
上述结果是预期的,它基于读取值。因此,该表是基于在创建的表上应用的读取值,偏移量和限制创建的。所以结果如上所示。
但我的要求是,offset和Limit应该适用于整个表,读取值应该适用于创建的表。
我需要查询预期结果。
答案 0 :(得分:2)
是的,它应该在最后。见https://dev.mysql.com/doc/refman/5.7/en/select.html
SELECT * FROM CUSTOMERS
ORDER BY somecolumn -- important to get consistent results
LIMIT 5 OFFSET 0
另一种做同样事情的方法是:
SELECT * FROM CUSTOMERS
ORDER BY somecolumn
LIMIT 0, 5
或在这种情况下(因为偏移量为0):
SELECT * FROM CUSTOMERS
ORDER BY somecolumn
LIMIT 5
答案 1 :(得分:2)
MariaDB [sandbox]> Drop table if exists sms_view;
Query OK, 0 rows affected (0.10 sec)
MariaDB [sandbox]> create table sms_view(SMS int,db_id int, `read` int);
Query OK, 0 rows affected (0.28 sec)
MariaDB [sandbox]> insert into sms_view values
-> (1, 2, 3) ,
-> (2, 2, 3),
-> (3, 2, 2) ,
-> (4, 2, 2) ,
-> (5, 2, 2) ,
-> (6, 2, 2) ,
-> (7, 2, 2) ,
-> (8, 2, 2) ,
-> (9, 2, 2) ,
-> (10, 2, 2);
Query OK, 10 rows affected (0.04 sec)
Records: 10 Duplicates: 0 Warnings: 0
MariaDB [sandbox]>
MariaDB [sandbox]> select sms from
-> (
-> SELECT * FROM sms_view LIMIT 5 OFFSET 0
-> ) s
-> WHERE `read` = 2;
+------+
| sms |
+------+
| 3 |
| 4 |
| 5 |
+------+
3 rows in set (0.00 sec)