我实际上是通过在循环中执行大量查询而遇到问题 它似乎不起作用,100%的时间只是查询数组中的最新数据。
CurrentSocket.biznessStatus.ListOfItemsToAdd = ["1", "4"];
if(Object.keys(CurrentSocket.biznessStatus.ListOfItemsToAdd).length >= 1){
for(var id in CurrentSocket.biznessStatus.ListOfItemsToAdd){
var readedIt = CurrentSocket.biznessStatus.ListOfItemsToAdd[id];
pool.getConnection(function(id, err, connection){
if(err) throw err;
console.log("doAction of ListOfItemsToAdd for itemID -> "+readedIt);
connection.query("SELECT COUNT(*) AS it FROM users_biznesspanel WHERE user_id = ? AND ui_id = ?", [CurrentSocket.user_id, readedIt], function(err, result){
if(err) console.log(err);
if(result[0]["it"] != 0){
connection.query("UPDATE users_biznesspanel SET nb = nb + 1 WHERE user_id = ? AND ui_id = ?", [CurrentSocket.user_id, readedIt], function(err, result){
connection.release();
if(err) console.log(err);
console.log("Added item: "+readedIt);
});
}
else{
connection.query("SELECT type FROM items WHERE iid = ?", [readedIt], function(err, result){
if(err) console.log(err);
var catInfo = getItemType(result[0]["type"]);
var insertItem = "INSERT INTO users_biznesspanel(user_id, ui_id, icat, iscat) VALUES(?, ?, ?, ?)";
connection.query(insertItem, [CurrentSocket.user_id, readedIt, catInfo.cat, catInfo.scat], function(err, result){
connection.release();
if(err) console.log(err);
console.log("Inserted item: "+readedIt);
});
});
}
});
}.bind(pool, id));
}
}
答案 0 :(得分:0)
我建议你在一个复杂的sql中查询SQL,你可以使用" case"语法,例如:
case sex
when '1' then '男'
when '2' then '女’
else '其他' end
我总是使用' sequelize' NodeJS中的模块帮我查询sql。它会更安全,你可以专注于你的后勤。