简体   繁体   English

SQL查询中的LIMIT返回指定的最大金额

[英]LIMIT in SQL query returning MAX amount specified

For some reason this is always returning 31 row instead of the expected 2 rows. 由于某种原因,它总是返回31行,而不是预期的2行。 Not really certain what is going on? 不确定是怎么回事吗?

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;

Expecting 期待

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

Your statement is telling MySQL to return 32 rows. 您的声明告诉MySQL返回32行。

LIMIT 30,32

That says "skip the first 30 rows, and then return the next 32 rows". 也就是说“跳过前30行,然后返回接下来的32行”。

If you want to skip the first 30 rows and return only the next 2 rows, your LIMIT clause needs to be modified. 如果要跳过前30行并仅返回后2行,则需要修改LIMIT子句。

You need to just replace the limit with 30,2. 您只需将限制替换为30,2。 Like below query 像下面的查询

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;
LIMIt 30, 32

Equals to 等于

Move the pointer to the 30th row, and get the next 32 rows. 将指针移到第30行,并获得下一个32行。

Change it to 更改为

LIMIT 30, 2

If you want it to return 2 rows. 如果要返回2行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM