限制条款与工会

时间:2014-08-11 09:35:15

标签: mysql

我有这种查询。我使用限制,但这个查询给了我20个结果。谁能告诉我为什么

SELECT 
    *,
    `tablename`.`bookmark_id` as `bookmark_id`,
    `tablename`.`bookmark_date` as `bookmark_date`  
FROM  ( (" + sql1 + ") 
UNION ALL (" + sql2 + ") 
UNION ALL (" + sql3 + ") ) AS tablename 
WHERE `bookmark_id` 
NOT IN 
   (SELECT `table1`.`bookmark_id` 
   FROM (
       (SELECT `user_bookmarks`.`bookmark_id` 
        FROM `user_bookmarks` 
        WHERE `user_bookmarks`.`bookmark_id` = `bookmark_id` 
        AND `user_id` = 26) 
        UNION 
       (SELECT `bookmark_id` 
        FROM `user_deleted_bookmarks` 
        WHERE `user_id` = ?)
       ) AS `table1`)  
GROUP BY bookmark_id 
ORDER BY `bookmark_date` 
DESC limit 17, 20

由于

3 个答案:

答案 0 :(得分:1)

来自SELECT docs

使用一个参数,该值指定从结果集开头返回的行数:

SELECT * FROM tbl LIMIT 5;     # Retrieve first 5 rows

使用两个参数,第一个参数指定要返回的第一行的偏移量,第二个参数指定要返回的最大行数。初始行的偏移量为0(不是1):

SELECT * FROM tbl LIMIT 5,10;  # Retrieve rows 6-15

我想你想要LIMIT 16, 4

答案 1 :(得分:1)

使用限制时,第二个参数是要返回的结果数。 请参阅:http://dev.mysql.com/doc/refman/5.0/en/select.html

根据您的情况,您需要:

SELECT .... LIMIT 16,4

您将获得4行:17,18,19和20。

答案 2 :(得分:0)

SELECT *,`tablename`.`bookmark_id` as `bookmark_id`,`tablename`.`bookmark_date` as `bookmark_date`  FROM  ( (" + sql1 + ") union all (" + sql2 + ") union all (" + sql3 + ") ) AS tablename WHERE `bookmark_id` NOT IN (SELECT `table1`.`bookmark_id` FROM ((SELECT `user_bookmarks`.`bookmark_id` FROM `user_bookmarks` WHERE `user_bookmarks`.`bookmark_id` = `bookmark_id` AND `user_id` = 26) UNION (SELECT `bookmark_id` FROM `user_deleted_bookmarks` WHERE `user_id` = ?)) AS `table1`)  GROUP BY bookmark_id ORDER BY `bookmark_date` 
DESC limit 16, 4;

获得结果17,18,19,20 它的计数从第17位开始,并给出接下来的4个值