我想问的第一个问题是,使用多个where子句进行一次删除查询会更快,还是一个接一个地执行更快? 如果是这样,那么如何使用多个where子句进行删除查询? 提前谢谢!
答案 0 :(得分:4)
您不能拥有多个WHERE
子句,但可以使条件更复杂。
如果您的表格有FirstName
和LastName
,并且您要删除John Doe
,Toby Steed
和Linda Powers
,请合并AND
和OR
这样的条件:
DELETE FROM MyTable
WHERE ( FirstName = 'John' AND LastName = 'Doe' )
OR ( FirstName = 'Toby' AND LastName = 'Steed' )
OR ( FirstName = 'Linda' AND LastName = 'Powers' )
现在,如果你是用Java做的,你不应该(永远!)使用字符串连接来构建这样的SQL语句。使用PreparedStatement
:
String sql = "DELETE FROM MyTable" +
" WHERE ( FirstName = ? AND LastName = ? )" +
" OR ( FirstName = ? AND LastName = ? )" +
" OR ( FirstName = ? AND LastName = ? )";
try (PreparedStatement stmt = conn.prepareStatement(sql)) {
stmt.setString(1, "John");
stmt.setString(2, "Doe");
stmt.setString(3, "Toby");
stmt.setString(4, "Steed");
stmt.setString(5, "Linda");
stmt.setString(6, "Powers");
stmt.executeUpdate();
}
答案 1 :(得分:2)
尝试使用这可能是您问题的解决方案:
db.delete(TABLE,
KEY1 + "=? AND " + KEY2 + "=? AND " +
KEY3 + "=? AND " + KEY4 + "=?",
new String[] {param1, param2, param3, param4});
如果使用普通选择,请使用:
db.delete(TABLE,"Column1='value1' and column2 like 'value2'...", null);