我想将三个查询合并为一个可以接受可变长度的WHERE参数的查询。我不记得我的一生如何做到这一点。

PreparedStatement queryOne = connection.prepareStatement
("SELECT columnOne, columnTwo, columnThree FROM tableOne WHERE columnOne = ?;" );

PreparedStatement queryTwo = connection.prepareStatement
("SELECT columnOne, columnTwo, columnThree FROM tableOne WHERE columnTwo = ?;" );

PreparedStatement queryThree = connection.prepareStatement
("SELECT columnOne, columnTwo, columnThree FROM tableOne WHERE columnOne = ? AND columnTwo = ?;" );

最佳答案

这三个查询都从同一张表中选择相同的列,因此可以很容易地在一条语句中完成它们的并集:

SELECT columnOne, columnTwo, columnThree
  FROM tableOne
 WHERE columnOne = ?
    or columnTwo = ?
    or (columnOne = ? AND columnTwo = ?)

10-01 02:23