SQL查询中的LIMIT返回指定的MAX量

时间:2014-09-17 18:26:49

标签: mysql

由于某种原因,这总是返回31行而不是预期的2行。不确定发生了什么事?

SELECT 
    ins_by          AS userid,
    wds_user_id     AS customer, 
    wds_address_id  AS billing,
    (
        SELECT 
            wds_address_id  AS shipping
        FROM 
            wds_user_address B
        WHERE 
            ins_by != "GUEST" 
        AND 
            wds_address_type_id = 2 
        AND 
            B.ins_by = A.ins_by
    ) AS shipping
FROM 
    wds_user_address A 
WHERE 
    ins_by != "GUEST" 
AND 
    wds_address_type_id = 1
ORDER BY id ASC 
LIMIT 30, 32;

期待

+---------------------+----------+---------+----------+
| userid              | customer | billing | shipping |
+---------------------+----------+---------+----------+
| FOO@GMAIL.COM       |      121 |     185 |      186 |
| BAR@GMAIL.COM       |      123 |     189 |      190 |
+---------------------+----------+---------+----------+

3 个答案:

答案 0 :(得分:2)

你的陈述告诉MySQL返回32行。

LIMIT 30,32

那说“跳过前30行,然后返回接下来的32行”。

如果要跳过前30行并仅返回接下来的2行,则需要修改LIMIT子句。

答案 1 :(得分:1)

您需要用30,2替换限制。如下面的查询

SELECT 
    ins_by          AS userid,
    wds_user_id     AS customer, 
    wds_address_id  AS billing,
    (
        SELECT 
            wds_address_id  AS shipping
        FROM 
            wds_user_address B
        WHERE 
            ins_by != "GUEST" 
        AND 
            wds_address_type_id = 2 
        AND 
            B.ins_by = A.ins_by
    ) AS shipping
FROM 
    wds_user_address A 
WHERE 
    ins_by != "GUEST" 
AND 
    wds_address_type_id = 1
ORDER BY id ASC 
LIMIT 30, 2;

答案 2 :(得分:1)

LIMIt 30, 32

等于

将指针移动到第30行,然后获得接下来的32行。

将其更改为

LIMIT 30, 2

如果您希望它返回2行。