具有不同比较器的SQL IN运算符

时间:2015-11-20 19:18:26

标签: sql oracle sql-in

我需要一种方法来创建一个IN运算符,为每个参数进行不同类型的比较,这样:

SELECT * 
FROM Table 
WHERE (par1, par2, par3) IN ((par1answer1, par2answer1, par3min1, par3max1),
                             (par1answer2, par2answer2, par3min2, par3max2),
                             ...,
                             (par1answern, par2answern, par3minn, par3maxn)

说明:

  • par1需要与par1answer1或par1answer2
  • 相等
  • par2需要与par2answer1或par2answer2
  • 相等
  • par3需要在par3min1和par3max1之间,或par3min2,par3max2
  • 之间

@EDIT
抱歉不清楚,我不是很擅长解释,但我正在努力! 示例集:

par1    par2    par3
2       5       10
3       6       20
4       7       30
5       8       60

SELECT * 
FROM Table 
WHERE (par1, par2, par3) IN ((2,5,9,11),
                             (3,6,11,19),
                             (5,7,9,100),
                             (5,8,10,80),
                             (2,8,0,200))

Result:
par1    par2    par3
2       5       10    //matches first tuple
5       8       60    //matches fourth tuple

2 个答案:

答案 0 :(得分:4)

我猜你真的想要这个:

WHERE ( (par1, par2) = (par1answer1, par2answer1) and
        par3 between par3min1 and par3max1
      ) or
      ( (par1, par2) = (par1answer2, par2answer2) and
        par3 between par3min2 and par3max2
      )

这不完全是你的解释如何,但它似乎是对你的尝试查询的合理解释。

答案 1 :(得分:2)

只需手动写出比较。 IN不会做你想做的事。

where (par1 = par1answer or par1 = par1answer2)
and (par2 = par2answer1 or par2 = par2answer2)
and (par3 between par3min1 and par3max1 or par3 between par3min2 and par3max2)