2016-04-27 58 views
-2

我有例如這樣的:選擇件的這種顏色不屬於一個車輪和質譜> = 10 和我有四個想法WHERE NOT SQL INSTRUCTION

  1. select color from A where not(name='wheel') and mass>=10
  2. select color from A where mass>=10 and not(name='wheel')
  3. select color from A where not name='wheel' and mass>=10
  4. select color from A where (not name='wheel') and mass>=10

哪一個是正確?我認爲第一個肯定沒問題,但其餘的呢?

+3

只需嘗試四個不同的版本。不要忘記在你的表中有一些NULL值。 (他們傾向於混淆初學者。) – jarlh

+1

從A中選擇顏色,其中名稱<>'wheel'且質量> = 10; –

+0

我知道<>和!=,但我必須使用NOT – tabaluga2012

回答

1

我通常只對關鍵詞(存在,類似)使用,並保留<>或!=進行反向測試。不知道這是一個慣例,但我幾乎每次看到這種查詢:

select color from A where name != 'wheel' and mass >= 10 

下面是語法的一個很好的參考:http://www.sqlstyle.guide/,一無所知NOT關鍵字的良好使用。

但是,您可以將您的4個查詢視爲「正確」。

圓括號在這裏有一個數學運算行爲,在每個例子中有或沒有相同的結果,因爲它只有一個元素。

0

使用此爲清楚起見

select color from A where name <> 'wheel' and mass >= 10 
0

當你只有一個表,它能夠更好地使用<>!=

0

只是把我的2個便士就這個問題,以及:

在任何RDBMS NOT<>運算符通常是查詢最耗時的任務。如果可能的話,避免它們總是很好的。有時候這是不可能的,但有時候是這樣。

以查詢爲例:如果name上表A具有非常低的基數,即你只能有幾個不同的names這是由設計決定,最好將它們全部列出,但「車輪」,因而避免NOT<>

select color 
from A 
where name IN ('something','somethingelse','anotherthing') 
    and mass >= 10