我在sql查询中出错。我收到错误消息:代码:“ ER_PARSE_ERROR”, errno:1064。
我尝试将其直接复制并粘贴到数据库中,并且效果很好,但是当我在使用mymysql的nodejs服务器上运行它时,我得到了错误。查询是:
SELECT ER.id_entidad as companyid, E.nombre as name, ER.posicion as position, E.url_imagen as url,ER.created
FROM
entidad_red ER, cajero C, red R, entidad E
WHERE E.id_red = R.id_red AND C.id_red = E.id_red AND E.id_entidad = ER.id_entidad AND C.Serial = 'id'
而sqlMessage是:
您的SQL语法有错误;检查与您的MySQL服务器版本相对应的手册以获取正确的语法,以在\'SELECT ER.id_entidad作为公司ID,E.nombre作为名称,ER.posicion作为位置,\'附近使用 1'。
感谢您的帮助
这是我执行查询的方式:
const dbConnection = mysql.createConnection(config);
app.get('/kioskcompanies', middleware.checkToken, function(req, res){
var serial = req.query.kioskid;
var sql = "SELECT ER.id_entidad as companyid, E.nombre as name, ER.posicion as position, E.url_imagen as url,ER.created " +
"FROM entidad_red ER, cajero C, red R, entidad E " +
"WHERE " +
"E.id_red = R.id_red " +
"AND C.id_red = E.id_red " +
"AND E.id_entidad = ER.id_entidad " +
"AND C.Serial = '" + serial + "'";
console.log(sql)
dbConnection.query(sql, (err, result) => {
if(err){
console.log(err);
res.status(200).json({
status:'error',
code:"1",
msg:'Error consultando la BD',
})
}
else if(result.length === 0){
res.status(200).json({
status:'error',
code:"3",
msg:'Entidad de recaudo no existe.',
})
}
else{
res.status(200).json({
status:'error',
code:'0',
companies:result,
msg:'Consulta correcta'
})
}
})
})