From 21d0277a7708bcddf14b01036a81910f9a0be0fe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=91=A3=E6=B5=B7=E6=B4=8B?= Date: Wed, 3 Jun 2026 14:22:20 +0800 Subject: [PATCH] =?UTF-8?q?=E6=8E=A5=E5=8F=A3=E8=AF=B7=E6=B1=82=E6=8A=A5?= =?UTF-8?q?=E9=94=99?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- utils/pagination.js | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) 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