App下載

SQL Where – 子句示例

伊萊尼亞·馬戈尼 2021-08-30 10:26:25 瀏覽數(shù) (2291)
反饋

有時(shí),當(dāng)您使用 SQL 時(shí),您不需要對整個(gè)記錄范圍進(jìn)行操作?;蛘撸绻恍⌒母幕騽h除了所有內(nèi)容,那將導(dǎo)致非常嚴(yán)重的后果。在這些情況下,您只需選擇要處理的記錄部分,即滿足特定條件的記錄部分。這就是 SQL 的WHERE子句有用的地方。

SQL WHERE子句語法

你這樣寫WHERE子句:

SELECT column1, column2...
FROM table_name
WHERE condition;

請注意,這里我使用SELECT語句編寫了它,但它的使用不僅限于SELECT. 您也可以將它與其他語句一起使用,例如DELETE和UPDATE。

演示數(shù)據(jù)庫

讓我們使用這個(gè)users表作為如何使用WHERE子句的示例。

IDNAMEAGESTATEEMAIL
1Brian15Michiganbrian@example.com
2Leonard55Mississippileonard@example.com
3Anvil31South Dakotaanvil@example.com
4Jo44Mainejo@example.com
5Meredith43Delawaremeredith@example.com
6Cody16Michigancody@example.com
7Dilara50Ohiodilara@example.com
8Corbin47Wisconsincorbin@example.com
9Gin63Illinoisgin@example.com
10Alice50Nevadaalice@example.com
11Zachary21Massachusettszachery@example.com
12Delmar56Idahodelmar@example.com
13Dennie96Ohiodennie@example.com
14Aaron50Floridaaaron@example.com
15Busrah18South Dakotabusrah@example.com
16Aveline88Nevadaaveline@example.com
17Aherin72Arkansasaherin@example.com
18Viola66Maineviola@example.com
19Nadya22Floridanadya@example.com
20Izabela61Arizonaizabela@example.com

SELECT帶有WHERE語句的 SQL子句  示例

當(dāng)您想確保某個(gè)事件會(huì)影響 50 歲或以上的人時(shí),您可以僅選擇具有以下代碼的用戶:

SELECT *
FROM users
WHERE age >= 50;

這將給出一個(gè)如下表,只列出 50 歲或以上的用戶:

IDNAMEAGESTATEEMAIL
2Leonard55Mississippileonard@example.com
7Dilara50Ohiodilara@example.com
9Gin63Illinoisgin@example.com
10Alice50Nevadaalice@example.com
12Delmar56Idahodelmar@example.com
13Dennie96Ohiodennie@example.com
14Aaron50Floridaaaron@example.com
16Aveline88Nevadaaveline@example.com
17Aherin72Arkansasaherin@example.com
18Viola66Maineviola@example.com
20Izabela61Arizonaizabela@example.com

DELETE帶有WHERE語句的 SQL子句示例

假設(shè) Cody 已決定將自己從該列表中刪除。您可以使用一條DELETE語句更新表,WHERE以確保只刪除 Cody 的記錄。

DELETE FROM users
WHERE name IS "Cody";

該users表現(xiàn)在看上去像下面,沒有線6(其中科迪的信息是):

IDNAMEAGESTATEEMAIL
1Brian15Michiganbrian@example.com
2Leonard55Mississippileonard@example.com
3Anvil31South Dakotaanvil@example.com
4Jo44Mainejo@example.com
5Meredith43Delawaremeredith@example.com
7Dilara50Ohiodilara@example.com
8Corbin47Wisconsincorbin@example.com
9Gin63Illinoisgin@example.com
10Alice50Nevadaalice@example.com
11Zachary21Massachusettszachery@example.com
12Delmar56Idahodelmar@example.com
13Dennie96Ohiodennie@example.com
14Aaron50Floridaaaron@example.com
15Busrah18South Dakotabusrah@example.com
16Aveline88Nevadaaveline@example.com
17Aherin72Arkansasaherin@example.com
18Viola66Maineviola@example.com
19Nadya22Floridanadya@example.com
20Izabela61Arizonaizabela@example.com

現(xiàn)在也許您已經(jīng)收到通知,說 Anvil 已經(jīng)老了,現(xiàn)在 32 歲了。您可以使用該UPDATE語句更改 Anvil 的記錄,并且您可以使用它WHERE來確保僅更新 Anvil 的記錄。

UPDATE帶有WHERE語句的 SQL子句示例

UPDATE users
SET age = 32
WHERE name IS "Anvil";

現(xiàn)在該表將如下所示:

IDNAMEAGESTATEEMAIL
1Brian15Michiganbrian@example.com
2Leonard55Mississippileonard@example.com
3Anvil32South Dakotaanvil@example.com
4Jo44Mainejo@example.com
5Meredith43Delawaremeredith@example.com
7Dilara50Ohiodilara@example.com
8Corbin47Wisconsincorbin@example.com
9Gin63Illinoisgin@example.com
10Alice50Nevadaalice@example.com
11Zachary21Massachusettszachery@example.com
12Delmar56Idahodelmar@example.com
13Dennie96Ohiodennie@example.com
14Aaron50Floridaaaron@example.com
15Busrah18South Dakotabusrah@example.com
16Aveline88Nevadaaveline@example.com
17Aherin72Arkansasaherin@example.com
18Viola66Maineviola@example.com
19Nadya22Floridanadya@example.com
20Izabela61Arizonaizabela@example.com

可以與WHERE子句一起使用的運(yùn)算符來選擇記錄

您可以使用=, >, <, >=, <=, <>(或!=取決于您的 SQL 版本), BETWEEN, LIKE,等運(yùn)算符IN。

我們已經(jīng)>=在上面的例子中看到了“大于或等于”的作用。

=是“等于”、>“大于”、<“小于”、<=“小于或等于”、<>(或!=)是“不等于”。

大于、小于、大于或等于、小于或等于四個(gè)運(yùn)算符在處理數(shù)字時(shí)最有用。

等于和不等于這兩個(gè)運(yùn)算符對于數(shù)字和其他數(shù)據(jù)類型都很有用。

如何在 SQL 中使用運(yùn)算符

BETWEEN允許您指定一個(gè)數(shù)字范圍。例如WHERE age BETWEEN 24 and 51將選擇該年齡范圍內(nèi)的所有記錄。

SELECT * FROM users
WHERE age BETWEEN 24 AND 51;

有 7 個(gè)用戶的年齡在此范圍內(nèi):

IDNAMEAGESTATEEMAIL
3Anvil32South Dakotaanvil@example.com
4Jo44Mainejo@example.com
5Meredith43Delawaremeredith@example.com
7Dilara50Ohiodilara@example.com
8Corbin47WIsconsincorbin@example.com
10Alice50Nevadaalice@example.com
14Aaron50Floridaaaron@example.com

如何在 SQL 中使用LIKE運(yùn)算符

LIKE允許您指定模式。例如,WHERE name LIKE "A%"將選擇名稱以 A 開頭的所有記錄。

SELECT * FROM users
WHERE name LIKE "A%";

在我們的列表中有 5 個(gè)名字以 A 開頭的用戶:

IDNAMEAGESTATEEMAIL
3Anvil32South Dakotaanvil@example.com
10Alice50Nevadaalice@example.com
14Aaron50Floridaaaron@example.com
16Aveline88Nevadaaveline@example.com
17Aherin72Arkansasaherin@example.com

如何制作要使用的圖案 LIKE

您可以使用字符%和制作圖案_。該字符%表示任意數(shù)量的字符(零、一個(gè)或多個(gè))。字符_正好代表一個(gè)字符。

例如"_ook"可以是“書”、“看”、“角落”。但"%ook"也可以是“ok”或“電話簿”。

如何IN在 SQL 中使用運(yùn)算符

IN讓您在一系列可能性之間進(jìn)行選擇。例如,讓我們看看哪些用戶在東海岸。

SELECT * FROM users
WHERE state IN ("Maine", "New Hampshire", "Massachusetts", "Rhode Island", "Connecticut", "New York", "New Jersey", "Delaware", "Maryland", "Virginia", "North Carolina", "South Carolina", "Georgia", "Florida");

該IN運(yùn)營商的檢查,如果在值state列等于在東海岸各州的列表中的一個(gè)值。

只有六個(gè)用戶住在東海岸:

IDNAMEAGESTATEEMAIL
4Jo44Mainejo@example.com
5Meredith43Delawaremeredith@example.com
11Zachery21Massachusettszachery@example.com
14Aaron50Floridaaaron@example.com
18Viola66Maineviola@example.com
19Nadya22Floridanadya@example.com

我們不要忘記IS, NOT, AND,OR運(yùn)算符

我們已經(jīng)IS在上面的示例之一中使用了運(yùn)算符。就像WHERE name IS "Cody",它檢查列是否具有該確切值。

您可以NOT在條件前使用使其相反。例如,WHERE age NOT BETWEEN 24 AND 51將僅選擇 24 歲以下和 51 歲以上的用戶。使用此標(biāo)準(zhǔn),將選擇 12 個(gè)用戶:

IDNAMEAGESTATEEMAIL
1Brian15Michiganbrian@example.com
2Leonard55Mississippileonard@example.com
9Gin63Illinoisgin@example.com
11Zachary21Massachusettszachery@example.com
12Delmar56Idahodelmar@example.com
13Dennie96Ohiodennie@example.com
15Busrah18South Dakotabusrah@example.com
16Aveline88Nevadaaveline@example.com
17Aherin72Arkansasaherin@example.com
18Viola66Maineviola@example.com
19Nadya22Floridanadya@example.com
20Izabela61Arizonaizabela@example.com

您可以使用AND到條件,結(jié)合這兩個(gè)必須是真實(shí)的,例如WHERE name LIKE "A%" AND age > 70會(huì)選擇使用一個(gè)名稱的用戶以A開頭的和是超過70只2用戶滿足這個(gè)條件:

IDNAMEAGESTATEEMAIL
16Aveline88Nevadaaveline@example.com
17Aherin72Arkansasaherin@example.com

您可以使用OR組合條件,以便只有兩者之一需要為真。例如,WHERE name LIKE "A%" OR age > 70將選擇名稱以 A 開頭或年齡超過 70 歲的用戶(兩部分中只有一個(gè)必須為真,但兩者也可以為真)。

有 6 個(gè)用戶名以 A 開頭或年齡超過 70 歲(或兩者兼有)。

IDNAMEAGESTATEEMAIL
3Anvil32South Dakotaanvil@example.com
10Alice50Nevadaalice@example.com
13Dennie96Ohiodennie@example.com
14Aaron50Floridaaaron@example.com
16Aveline88Nevadaaveline@example.com
17Aherin72Arkansasaherin@example.com

指定要在表中操作的記錄非常重要。

結(jié)論

通過本文,您已經(jīng)學(xué)會(huì)了如何使用該WHERE子句來做到這一點(diǎn)。

感謝您的閱讀!


SQL

0 人點(diǎn)贊