假设我有一个由5列组成的DB2表,我应该如何根据每个列的已知值的任意组合来过滤行呢?
如果我对5列中的每一列都有一个搜索值,并且我想返回与所有这些值匹配的行,那么我可以简单地按以下条件进行筛选
SELECT * FROM TABLE
WHERE A = value1 and B = value2 and C = value3 and D = value 4 and E = value 5
但是,如果我只有来自列A和B、A和C、A B和C等的搜索值,该怎么办?如何创建一个WHERE子句,使其能够根据我拥有的可用搜索值筛选返回的行?
如何实现输入的搜索条件越多,在一个WHERE子句中返回的行就越少的想法?
1条答案
按热度按时间ar5n3qh51#
这取决于特定的数据库解析器,但是如果您有五列
a
、b
、c
、d
、e
和五个可能为空的参数p0
、p1
、p2
、p3
、p4
,则查询可以采用以下形式:使用JDBC时,参数化查询可能如下所示:
请记住,像这样的查询提供的优化机会很少。很可能引擎每次都需要扫描整个堆。