diff --git a/utils/pagination.js b/utils/pagination.js index 2eca2d1..f4a7992 100644 --- a/utils/pagination.js +++ b/utils/pagination.js @@ -16,8 +16,13 @@ async function paginate(queryFn, sql, params, page = 1, pageSize = 20) { const p = Math.max(1, parseInt(page) || 1) const ps = Math.min(100, Math.max(1, parseInt(pageSize) || 20)) + // 分离 ORDER BY,避免子查询中不允许 ORDER BY 导致报错 + const orderByMatch = sql.match(/\s+ORDER\s+BY\s+.+$/i) + const orderBy = orderByMatch ? orderByMatch[0] : '' + const countSql = orderBy ? sql.slice(0, orderByMatch.index) : sql + const countResult = await queryFn( - `SELECT COUNT(*) as total FROM (${sql}) AS _paged`, + `SELECT COUNT(*) as total FROM (${countSql}) AS _paged`, params ) const total = countResult[0].total