我从表中有两个列名foo
和bar
。我想返回任何在X
和Y
条件下返回true的行。我想将下表中的条件合并到我的select
查询的where
部分。SQL - 在两个变量上混合使用XOR和AND的查询
X | Y | conditions
----------+----------+----------------
null | null |
not null | null | foo=X
null | not null | bar=Y
not null | not null | foo=X and bar=Y
这里X
和Y
是得到替换到查询变量。它们都不会同时为空,所以我们不关心第一个条件。但是如果一个为空,则该条件检查另一个(XOR)。如果两者都不为空,则条件检查两者(AND)。 这是我到目前为止的查询,但有没有更简单,更清洁的where
部分?
select
...
where
(X is not null and Y is not null and
foo=X and bar=Y)
or (X is not null and
Y is null and foo=X)
or (X is null and
Y is not null and bar=Y)
编辑注:foo
和bar
可能具有空值。
您是否想同时显示两个空变量? – OnlyMAJ 2014-11-21 02:38:53
@OnlyMAJ永远不应该是这样。 – Julio 2014-11-21 04:55:38