在node-mysql中选择WHERE IN

Tak*_*chi 6 javascript node.js node-mysql

有谁知道如何SELECT WHERE IN在node-mysql中使用?

我已经尝试了下面的代码,但是我收到以下错误消息:

'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 ''(`PHP`,`apache`)'' at line 1'
Run Code Online (Sandbox Code Playgroud)

这是我的代码:

whereIn = '(';
for ( var i in tagArray ) {
    if ( i != tagArray.length - 1 ) {
        whereIn += "`" + tagArray[i] + "`,";    
    }else{
        whereIn += "`" + tagArray[i] + "`"; 
    }
}
whereIn += ')';

console.log(whereIn);

client.query(
    'SELECT tag_id FROM tag WHERE tag_name IN ?',
    [whereIn],
    function(err, result, fields) {
        client.destroy();

        if (err) {
            throw err;
        }

        console.log(result);

        res.redirect('/');
    }
);
Run Code Online (Sandbox Code Playgroud)

Dan*_*ara 16

你必须使用IN (?)和不IN ?.

任何字符串操作都可能导致SQL INJECTION后门.


gco*_*ard 2

您需要引用字符串,而不是使用反引号。

whereIn = '(';
for ( var i in tagArray ) {
    if ( i != tagArray.length - 1 ) {
        whereIn += "'" + tagArray[i] + "',";    
    }else{
        whereIn += "'" + tagArray[i] + "'"; 
    }
 }
whereIn += ')';
Run Code Online (Sandbox Code Playgroud)