分页查询

优质
小牛编辑
137浏览
2023-12-01

limt 分页查询

查询前3条记录

分页公式: (offset - 1) * limit

SELECT name  FROM `student`  LIMIT 0, 3;

Student.findAll( {
    attributes:['name'],
    // 当前页
    offset:0,
    // 每页显示的条数
    limit:3
})

分页案例

router.get('/getUserList', async (ctx, next) => {

    let { currentPage=1, count=10 } = ctx.request.header;

    const userList = await models.user.findAllAndCount({
        limit: parseInt( count ),
        offset:(currentPage - 1) * count,
        include: [{
            model: { explore },
            as: 'order_info'
        }],
        distinct: true

    }).then(res => {
        let result = {};
        result.data = res.rows;
        result.totalCount = res.count;
        return result;
    });

    ctx.body = userList;
});