express.js, MySQL 分页
express.js, MySQL pagination
我正在尝试在服务器上分页,但我从 MySQL 收到语法错误。
抱歉,我是新手,所以我不太明白问题在哪里...
这是我的代码:
(FullUrl 是 BASE_URL 和 url 的组合 - 此 table 中的列)
router.get('/', function (req, res) {
let numRows;
let numPerPage = parseInt(req.query.npp, 10) || 40;
let page = parseInt(req.query.page, 10) || 0;
let numPages;
let skip = page * numPerPage;
let limit = skip + ',' + numPerPage;
queryAsync(`SELECT count(*, concat('${BASE_URL}/', url) as FullUrl) FROM urls as numRows`)
.then(function(results) {
numRows = results[0].numRows;
numPages = Math.ceil(numRows / numPerPage);
console.log('number of pages:', numPages + 'number of rows:', numRows);
})
.then(() => queryAsync(`SELECT *, concat('${BASE_URL}/', url) as FullUrl FROM urls LIMIT ` + limit))
.then(function(results) {
const responsePayload = {
name: 'marketing_campaign',
data: results
};
if (page < numPages) {
responsePayload.pagination = {
current: page,
perPage: numPerPage,
previous: page > 0 ? page - 1 : null,
next: page < numPages - 1 ? page + 1 : null,
pages: numPages
}
}
else responsePayload.pagination = {
err: 'queried page ' + page + ' is >= to maximum page number ' + numPages
}
res.json(responsePayload);
})
.catch(function(err) {
console.error(err);
res.json({ err: err });
});
});
MySQL 错误:
{
"err": {
"cause": {
"code": "ER_PARSE_ERROR",
"errno": 1064,
"sqlMessage": "You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ', concat('http://localhost:3001/', url) as FullUrl) FROM urls as numRows' at line 1",
"sqlState": "42000",
"index": 0,
"sql": "SELECT count(*, concat('http://localhost:3001/', url) as FullUrl) FROM urls as numRows"
},
"isOperational": true,
"code": "ER_PARSE_ERROR",
"errno": 1064,
"sqlMessage": "You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ', concat('http://localhost:3001/', url) as FullUrl) FROM urls as numRows' at line 1",
"sqlState": "42000",
"index": 0,
"sql": "SELECT count(*, concat('http://localhost:3001/', url) as FullUrl) FROM urls as numRows"
}
}
你可以关闭括号。
但既然您只想要计数,为什么还要添加另一列?
queryAsync(`SELECT count(*) as numRows FROM urls as numRows`)
我正在尝试在服务器上分页,但我从 MySQL 收到语法错误。
抱歉,我是新手,所以我不太明白问题在哪里...
这是我的代码: (FullUrl 是 BASE_URL 和 url 的组合 - 此 table 中的列)
router.get('/', function (req, res) {
let numRows;
let numPerPage = parseInt(req.query.npp, 10) || 40;
let page = parseInt(req.query.page, 10) || 0;
let numPages;
let skip = page * numPerPage;
let limit = skip + ',' + numPerPage;
queryAsync(`SELECT count(*, concat('${BASE_URL}/', url) as FullUrl) FROM urls as numRows`)
.then(function(results) {
numRows = results[0].numRows;
numPages = Math.ceil(numRows / numPerPage);
console.log('number of pages:', numPages + 'number of rows:', numRows);
})
.then(() => queryAsync(`SELECT *, concat('${BASE_URL}/', url) as FullUrl FROM urls LIMIT ` + limit))
.then(function(results) {
const responsePayload = {
name: 'marketing_campaign',
data: results
};
if (page < numPages) {
responsePayload.pagination = {
current: page,
perPage: numPerPage,
previous: page > 0 ? page - 1 : null,
next: page < numPages - 1 ? page + 1 : null,
pages: numPages
}
}
else responsePayload.pagination = {
err: 'queried page ' + page + ' is >= to maximum page number ' + numPages
}
res.json(responsePayload);
})
.catch(function(err) {
console.error(err);
res.json({ err: err });
});
});
MySQL 错误:
{
"err": {
"cause": {
"code": "ER_PARSE_ERROR",
"errno": 1064,
"sqlMessage": "You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ', concat('http://localhost:3001/', url) as FullUrl) FROM urls as numRows' at line 1",
"sqlState": "42000",
"index": 0,
"sql": "SELECT count(*, concat('http://localhost:3001/', url) as FullUrl) FROM urls as numRows"
},
"isOperational": true,
"code": "ER_PARSE_ERROR",
"errno": 1064,
"sqlMessage": "You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ', concat('http://localhost:3001/', url) as FullUrl) FROM urls as numRows' at line 1",
"sqlState": "42000",
"index": 0,
"sql": "SELECT count(*, concat('http://localhost:3001/', url) as FullUrl) FROM urls as numRows"
}
}
你可以关闭括号。
但既然您只想要计数,为什么还要添加另一列?
queryAsync(`SELECT count(*) as numRows FROM urls as numRows`)