2012-12-19 47 views
0

關於之間的區別...瞭解,Postgres的查詢

select * from table_a where id != 30 and name != 'Kevin'; 

select * from table_a where id != 30 or name != 'Kevin'; 

首先一個手段,"select all rows from table_a where the id is not 30 and the name is not Kevin"

因此{30,'Bill'}的{Id,Name}行將從此第一個查詢中返回。

但是,第二個意思是,"select all rows from table_a where the id is not 30 or the name is not 'Kevin'"

因此,從第二個查詢返回上述{30,'Bill'}將而不是

是嗎?

回答

2
select * from table_a where id != 30 and name != 'Kevin'; 

所以{ID,名稱} {30, '比爾'}的列就會從這個第一 查詢返回。

不,它不會。

select * from table_a where id != 30 or name != 'Kevin'; 

因此上述{30, '比爾'}將從本次 查詢返回。

不,它會的。你有邏輯倒退。去嘗試一下。

+0

謝謝,@Erwin。 –

0

沒有。第二個查詢意味着「選擇ID不是30的所有行或者名稱不是'Kevin'」,因此「Bill」的名稱使記錄符合列入查詢的條件。

0

回顧:

A B not(A) not(B)  AND  OR 
1 1 0   0   0  0 
1 0 0   1   0  1 
0 1 1   0   0  1 
0 0 1   1   1  1 

所以,這兩個查詢的將返回相同的行僅當:

1 ID = 30和名稱= '凱文'

2-id!= 30 and name!='Kevin'

0

快速邏輯表達式轉換提示:

NOT(A和B)== NOT A OR否B

NOT(A或B)== NOT A與非B

+0

您應該將此與所問的問題聯繫起來。爲什麼德摩根適用於此? http://en.wikipedia.org/wiki/De_Morgan%27s_laws –