如何在Loopback中设置查询的起始偏移量与数量限制?
Hey there! I see you're trying to replicate the MySQL query SELECT * FROM ace_new.customer_profile LIMIT 10,5 (skip the first 10 rows and fetch the next 5) in LoopBack, and you've already got the limit part working. Let me show you the missing piece here.
In LoopBack, to achieve the equivalent of MySQL's LIMIT offset, row_count syntax, you need to use two query options together:
limit: Specifies the number of rows to fetch (this is the second value in your MySQL LIMIT clause, which is 5 here)offset: Specifies how many rows to skip before starting to fetch (this is the first value in your MySQL LIMIT clause, which is 10 here)
Here's the corrected code for your scenario:
weapons.find({ order: 'id DESC', limit: 5, // Fetch 5 rows offset: 10 // Skip the first 10 rows });
When you run this query, LoopBack will generate the exact MySQL statement you're targeting: SELECT * FROM weapons ORDER BY id DESC LIMIT 10, 5 (assuming your model maps to the weapons table).
This combination of offset and limit is the standard way to implement pagination or offset-based fetching in LoopBack for relational databases like MySQL.
内容的提问来源于stack exchange,提问作者Ginto Joseph

