-1

给定表中相同的行。这是 SQL 查询:

select * from table where (a in (0,1) and b in (0,2) and c in (0,3))

逻辑上等价于这个:

select * from table where
(a = 0 and b = 0 and c = 0)
or 
(a = 1 and b = 0 and c = 0)
or 
(a = 0 and b = 2 and c = 0)
or 
(a = 0 and b = 0 and c = 3)
or 
(a = 1 and b = 2 and c = 0)
or 
(a = 0 and b = 2 and c = 3)
or 
(a = 1 and b = 0 and c = 3)
or 
(a = 1 and b = 2 and c = 3);

???

4

2 回答 2

2

既然你问的是逻辑等价...

IN转换为一系列OR',所以:

a in (0,1)    →    a = 0 or a = 1
b in (0,2)    →    b = 0 or b = 2
c in (0,3)    →    c = 0 or c = 3

进行以下替换:

a = 0 → A 0 
a = 1 → A 1 
b = 0 → B 0 
b = 2 → B 2 
c = 0 → C 0 
c = 3 → C 3
和 → ∧
或 → ∨

并考虑与析取合取的分布性,即

X ∧ (Y ∨ Z)  ⇔  (X ∧ Y) ∨ (X ∧ Z)

你得到这个:

  (A 0 ∨ A 1 ) ∧ (B 0 ∨ B 2 ) ∧ (C 0 ∨ C 3 ) ⇒
⇒ ((A 0 ∨ A 1 ) ∧ (B 0 ∨ B 2 ) ∧ C 0 ) ∨ ((A 0 ∨ A 1 ) ∧ (B 0 ∨ B 2 ) ∧ C 3 ) ⇒
⇒ ((A 0 ∨ A 1 ) ∧ B 0 ∧ C 0 ) ∨ ((A 0 ∨ A 1 ) ∧ B 2 ∧ C 0 ) ∨
∨ ((A 0 ∨ A 1 ) ∧ B 0 ∧ C 3 ) ∨ ((A 0 ∨ A 1 ) ∧ B 2 ∧ C 3 ) ⇒
⇒ (A 0 ∧ B 0 ∧ C 0 ) ∨ (A 1 ∧ B 0 ∧ C 0 ) ∨ (A 0 ∧ B 2 ∧ C 0 ) ∨ (A 1 ∧ B 2 ∧ C 0 ) ∨
∨ (A 0 ∧ B 0 ∧ C 3 ) ∨ (A 1 ∧ B 0 ∧ C 3 ) ∨ (A 0 ∧ B 2 ∧ C 3 ) ∨ (A 1 ∧ B 2 ∧ C 3 )

将最终结果翻译回来,您将获得第二个查询的条件。

于 2013-10-29T20:37:27.930 回答
0

逻辑上?是的。但它们的性能可能大不相同。第一个只需要检查三个条件(最坏的情况),第二个可能必须检查八个。并不是说一个比另一个更好,因为在确定性能时有很多因素。

于 2013-10-29T18:31:53.290 回答