2017-06-18 40 views
3

我需要做一個能夠從我的表中刪除多行的查詢。爲了做到這一點,我在數組中創建了一個數組,其中需要傳遞給該查詢的值。這裏是我的代碼:節點JS mysql用陣列數組刪除多行

var deleteRooms = [ [ 3, 23 ], [ 4, 23 ], [ 5, 23 ], [ 2, 23 ]]; 

connection.query("DELETE FROM rate_plans_rooms WHERE room_id = ? AND rate_plan_id = ? ", 
[deleteRooms],function(err,results){ 
if(err){return console.log(err)} 

else 
{           
    console.log('sended'); 
} 

}); 

但每次我收到這樣的錯誤:

{ Error: ER_PARSE_ERROR: You have an error in your SQL syntax; check 
the manual that corresponds to your MariaDB server version for the 
right syntax to use near ' (4, 23), (5, 23), (2, 23) AND rate_plan_id 
= ?' at line 1 

我怎樣才能解決這個問題並正確發送我的查詢?

回答

3

用於您的問題的解決方案是使用「IN」的查詢中:

var deleteRooms = [[3,23],[4,23],[5,23], [2,23]]; 

connection.query("DELETE FROM rate_plans_rooms WHERE (room_id, rate_plan_id) IN (?)", 
    [deleteRooms],function(err,results){ 

    if(err) return console.log(err) 
    else console.log('sended'); 
}); 
+1

非常感謝你的幫助!你救了我的命! – andrzej541