I've been trying to get this query result to save into a variable but no luck. When I run the query from workbench directly I get the result,also if I run it from node, I get the following
set @count=(select column from table where value = 'value');
SET @row_number = 0;
select column from
(SELECT
(@row_number:=@row_number + 1) AS num,
column
FROM
table where column is null
ORDER BY column)as a
where num <= @count
this code basically selects X number of rows from tableA according to what ever value is saved to @count
var dnum = [];
dbconn.query('SET @row_number = 0;' +
'set @count=(select column from table where column= "value");' +
'select column from \n '+
'(SELECT \n' +
' (@row_number:=@row_number + 1) AS num, \n' +
' column \n' +
'FROM \n' +
' table where column is null \n' +
'ORDER BY column )as a \n' +
'where num <= @count ;',function(err,rows,result) {
console.log(rows)
dnum = rows or //dnum.push(rows)
})
console.log(dnum)
Sample output:
[
OkPacket {
fieldCount: 0,
affectedRows: 0,
insertId: 0,
serverStatus: 10,
warningCount: 0,
message: '',
protocol41: true,
changedRows: 0
},
OkPacket {
fieldCount: 0,
affectedRows: 0,
insertId: 0,
serverStatus: 10,
warningCount: 0,
message: '',
protocol41: true,
changedRows: 0
},
[
RowDataPacket { number: '123456789' },
RowDataPacket { number: '098745612' }
]
]
I would like to save the returning RowDataPacket results to a variable to use somewhere else.
Thanks
Should be as easy as:
rows.forEach(row => dnum.push(row.number));
If you're getting multiple results back from multiple queries, you may need to skip to the correct one. This could be done by destructuring the response:
dbconn.query('...', ,function(err,[ _ok1, _ok2, rows ], result) {
rows.forEach(row => dnum.push(row.number));
}
Where _ok1
and _ok2
are there to just sponge up the "don't care" responses to your SET
statements.
Thanks @tadman for all the comments. Apparently my previous query approach did return the expected result, but somehow it was not usable. I was able to achieve this via below code. Not sure how clean or sensible the code is,but it takes care of the job for now.Until I mess up somewhere else:)
function getNumbers(){
return new Promise(function(resolve, reject) {
var q = queue ///This part is returned from express frontend
let sql1 = 'set @count=(select number from table where name= ? )';
let sql2 = 'SET @row_number = 0';
let sql3 = 'select number from \n '+
'(SELECT \n' +
' (@row_number:=@row_number + 1) AS num, \n' +
' number \n' +
'FROM \n' +
' numbers where isSelected is null \n' +
'ORDER BY number)as a \n' +
'where num <= @count ;'
dbconn.query(sql1,q, (err, fields) => {
if (err) {
return reject(err);
}
});
dbconn.query(sql2, (err,fields) => {
if (err) {
return reject(err);
}
})
dbconn.query(sql3, (err, rows, fields) => {
if (err) {
return reject(err);
}
resolve(rows);
})
})
}
//////////////////////////return number//////////////////////////////
let dnum =[];
getNumbers().then((rows) => {
for (let i = 0; i < rows.length; i++)
// console.log(rows[i].number)
dnum.push(rows[i].number)
console.log(dnum)
})
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.