我在NodeJS中遇到此查询时遇到问题。
SET @i = 0;
SELECT POSITION FROM (
SELECT name, @i:=@i+1 AS POSITION
FROM database.table ti WHERE name='Johny' ORDER BY points ASC) t WHERE name='Johny'
查询在Heidi SQL中运行没有问题,但是当我在Node中执行它时,我得到的回调是未定义的。
NodeJS代码:
var query =
"SET @i = 0;"
+ " SELECT POSITION FROM ("
+ " SELECT name, @i:=@i+1 AS POSITION"
+ " FROM database.table ti WHERE name='Johny' ORDER BY points ASC) t WHERE name='Johny'";
mySQLconnection.query(query,function(err,rows){
console.log(rows);
});
提前谢谢你,
答案 0 :(得分:2)
我通过谷歌搜索这篇文章,我发现变量不起作用的原因 试试这个对我有用:
<select ng-model="vm.selectedValue">
<option value="" selected disabled>-- SELECT ONE --</option>
<option ng-repeat="option in options"
value="$index"
ng-selected="option === vm.selectedValue">
{{option.label}}
</option>
</select>
默认情况下,node-mysql只会按时执行一个查询 如果您在创建连接设置上启用了multipleStatements,那么您的代码应该可以正常工作。 希望它对你有用。
答案 1 :(得分:0)
我必须单独运行查询才能让它们工作。
如,
var config = {user: "root", password: "root"}; // your config
var sql1 = "SET @i = 0";
var sql2 = " SELECT POSITION FROM ( " +
"SELECT name, @i:=@i+1 AS POSITION " +
"FROM database.table ti WHERE name='Johny' ORDER BY points ASC) t " +
"WHERE name='Johny'";
var result = [];
var connection = mysql.createConnection(config);
var query1 = connection.query(sql1);
query1.on("error", function (err) {
// handle error
});
query1.on("end", function () {
var query2 = connection.query(sql2);
query2.on("error", function (err) {
// handle error
});
query2.on("result", function (row) {
result.push(row);
});
query2.on("end", function () {
connection.end(); // close connection
console.log(result); // display result
});
});
希望这有帮助。