這是用戶在 2025-8-7 19:23 為 https://www.coursera.org/learn/linux-and-sql/supplement/jZyoY/more-on-filters-with-and-or-and-not 保存的雙語快照頁面,由 沉浸式翻譯 提供雙語支持。了解如何保存?

More on filters with AND, OR, and NOT
更多關於使用 AND、OR 和 NOT 的篩選條件

Previously, you explored how to add filters containing the AND, OR, and NOT operators to your SQL queries. In this reading, you'll continue to explore how these operators can help you refine your queries.
先前,您已經探討過如何在 SQL 查詢中添加包含 AND、OR 和 NOT 運算符的篩選器。在這篇閱讀中,您將繼續探索這些運算符如何幫助您優化查詢。

Logical operators  邏輯運算符

AND, OR, and NOT allow you to filter your queries to return the specific information that will help you in your work as a security analyst. They are all considered logical operators.
AND、OR 和 NOT 允許您篩選查詢,以返回能夠幫助您作為安全分析師工作的特定資訊。它們都被視為邏輯運算符。

AND

First, AND is used to filter on two conditions. AND specifies that both conditions must be met simultaneously. 
首先,AND 用於篩選兩個條件。AND 指定必須同時滿足兩個條件。

As an example, a cybersecurity concern might affect only those customer accounts that meet both the condition of being handled by a support representative with an ID of 5 and the condition of being located in the USA. To find the names and emails of those specific customers, you should place the two conditions on either side of the AND operator in the WHERE clause:
舉例來說,一個網路安全問題可能只影響那些同時滿足由 ID 為 5 的客服代表處理和位於美國這兩個條件的客戶帳戶。要查找這些特定客戶的姓名和電子郵件,您應該在 WHERE 子句中將這兩個條件放在 AND 運算符的兩側:

+-----------+----------+-------------------------+---------+--------------+ | FirstName | LastName | Email | Country | SupportRepId | +-----------+----------+-------------------------+---------+--------------+ | Jack | Smith | jacksmith@microsoft.com | USA | 5 | | Kathy | Chase | kachase@hotmail.com | USA | 5 | | Victor | Stevens | vstevens@yahoo.com | USA | 5 | | Julia | Barnett | jubarnett@gmail.com | USA | 5 | +-----------+----------+-------------------------+---------+--------------+

Running this query returns four rows of information about the customers. You can use this information to contact them about the security concern.
執行此查詢會返回四行有關客戶的信息。你可以使用這些信息來聯繫他們,告知安全問題。

OR   

The OR operator also connects two conditions, but OR specifies that either condition can be met. It returns results where the first condition, the second condition, or both are met.
OR 運算符也連接兩個條件,但 OR 指定任一條件可以被滿足。它返回滿足第一個條件、第二個條件或兩者的結果。

For example, if you are responsible for finding all customers who are either in the USA or Canada so that you can communicate information about a security update, you can use an OR operator to find all the needed records. As the following query demonstrates, you should place the two conditions on either side of the OR operator in the WHERE clause:
例如,如果你負責查找所有位於美國或加拿大的客戶,以便傳達有關安全更新的信息,你可以使用 OR 運算符來找到所有需要的記錄。如下查詢所示,你應該將兩個條件放在 WHERE 子句中的 OR 運算符兩側:

+-----------+------------+--------------------------+---------+ | FirstName | LastName | Email | Country | +-----------+------------+--------------------------+---------+ | François | Tremblay | ftremblay@gmail.com | Canada | | Mark | Philips | mphilips12@shaw.ca | Canada | | Jennifer | Peterson | jenniferp@rogers.ca | Canada | | Frank | Harris | fharris@google.com | USA | | Jack | Smith | jacksmith@microsoft.com | USA | | Michelle | Brooks | michelleb@aol.com | USA | | Tim | Goyer | tgoyer@apple.com | USA | | Dan | Miller | dmiller@comcast.com | USA | | Kathy | Chase | kachase@hotmail.com | USA | | Heather | Leacock | hleacock@gmail.com | USA | | John | Gordon | johngordon22@yahoo.com | USA | | Frank | Ralston | fralston@gmail.com | USA | | Victor | Stevens | vstevens@yahoo.com | USA | | Richard | Cunningham | ricunningham@hotmail.com | USA | | Patrick | Gray | patrick.gray@aol.com | USA | | Julia | Barnett | jubarnett@gmail.com | USA | | Robert | Brown | robbrown@shaw.ca | Canada | | Edward | Francis | edfrancis@yachoo.ca | Canada | | Martha | Silk | marthasilk@gmail.com | Canada | | Aaron | Mitchell | aaronmitchell@yahoo.ca | Canada | | Ellie | Sullivan | ellie.sullivan@shaw.ca | Canada | +-----------+------------+--------------------------+---------+

The query returns all customers in either the US or Canada.
查詢返回所有位於美國或加拿大的客戶。

Note: Even if both conditions are based on the same column, you need to write out both full conditions. For instance, the query in the previous example contains the filter WHERE country = 'Canada' OR country = 'USA'.
注意:即使兩個條件基於相同的欄位,你也需要寫出兩個完整的條件。例如,前一個例子中的查詢包含了篩選條件 WHERE country = 'Canada' OR country = 'USA'。

NOT

Unlike the previous two operators, the NOT operator only works on a single condition, and not on multiple ones. The NOT operator negates a condition. This means that SQL returns all records that don’t match the condition specified in the query. 
與前兩個運算符不同,NOT 運算符僅適用於單一條件,而非多個條件。NOT 運算符會否定一個條件。這意味著 SQL 返回所有不符合查詢中指定條件的記錄。

For example, if a cybersecurity issue doesn't affect customers in the USA but might affect those in other countries, you can return all customers who are not in the USA. This would be more efficient than creating individual conditions for all of the other countries. To use the NOT operator for this task, write the following query and place NOT directly after WHERE
例如,如果某個網路安全問題不影響美國的客戶,但可能影響其他國家的客戶,您可以返回所有不在美國的客戶。這比為所有其他國家創建個別條件更有效率。要使用 NOT 運算符來完成此任務,請撰寫以下查詢並將 NOT 直接放在 WHERE 之後
:

+-----------+-------------+-------------------------------+----------------+ | FirstName | LastName | Email | Country | +-----------+-------------+-------------------------------+----------------+ | Luís | Gonçalves | luisg@embraer.com.br | Brazil | | Leonie | Köhler | leonekohler@surfeu.de | Germany | | François | Tremblay | ftremblay@gmail.com | Canada | | Bjørn | Hansen | bjorn.hansen@yahoo.no | Norway | | František | Wichterlová | frantisekw@jetbrains.com | Czech Republic | | Helena | Holý | hholy@gmail.com | Czech Republic | | Astrid | Gruber | astrid.gruber@apple.at | Austria | | Daan | Peeters | daan_peeters@apple.be | Belgium | | Kara | Nielsen | kara.nielsen@jubii.dk | Denmark | | Eduardo | Martins | eduardo@woodstock.com.br | Brazil | | Alexandre | Rocha | alero@uol.com.br | Brazil | | Roberto | Almeida | roberto.almeida@riotur.gov.br | Brazil | | Fernanda | Ramos | fernadaramos4@uol.com.br | Brazil | | Mark | Philips | mphilips12@shaw.ca | Canada | | Jennifer | Peterson | jenniferp@rogers.ca | Canada | | Robert | Brown | robbrown@shaw.ca | Canada | | Edward | Francis | edfrancis@yachoo.ca | Canada | | Martha | Silk | marthasilk@gmail.com | Canada | | Aaron | Mitchell | aaronmitchell@yahoo.ca | Canada | | Ellie | Sullivan | ellie.sullivan@shaw.ca | Canada | | João | Fernandes | jfernandes@yahoo.pt | Portugal | | Madalena | Sampaio | masampaio@sapo.pt | Portugal | | Hannah | Schneider | hannah.schneider@yahoo.de | Germany | | Fynn | Zimmermann | fzimmermann@yahoo.de | Germany | | Niklas | Schröder | nschroder@surfeu.de | Germany | +-----------+-------------+-------------------------------+----------------+ (Output limit exceeded, 25 of 46 total rows shown)

SQL returns every entry where the customers are not from the USA.
SQL 返回所有客戶不是來自美國的條目。

Pro tip: Another way of finding values that are not equal to a certain value is by using the <> operator or the != operator. For example, WHERE country <> 'USA' and WHERE country != 'USA' are the same filters as WHERE NOT country = 'USA'.
專業提示:另一種尋找不等於某個值的方法是使用 <> 運算符或 != 運算符。例如,WHERE country <> 'USA' 和 WHERE country != 'USA' 與 WHERE NOT country = 'USA' 是相同的篩選條件。

Combining logical operators
結合邏輯運算符

Logical operators can be combined in filters. For example, if you know that both the USA and Canada are not affected by a cybersecurity issue, you can combine operators to return customers in all countries besides these two. In the following query, NOT is placed before the first condition, it's joined to a second condition with AND, and then NOT is also placed before that second condition. You can run it to explore what it returns:
邏輯運算符可以在篩選器中結合使用。例如,如果你知道美國和加拿大都不受某個網路安全問題的影響,你可以結合運算符來返回除這兩個國家以外的所有國家的客戶。在以下查詢中,NOT 放在第一個條件之前,並用 AND 連接到第二個條件,然後 NOT 也放在第二個條件之前。你可以運行它來探索它返回的結果:

+-----------+-------------+-------------------------------+----------------+ | FirstName | LastName | Email | Country | +-----------+-------------+-------------------------------+----------------+ | Luís | Gonçalves | luisg@embraer.com.br | Brazil | | Leonie | Köhler | leonekohler@surfeu.de | Germany | | Bjørn | Hansen | bjorn.hansen@yahoo.no | Norway | | František | Wichterlová | frantisekw@jetbrains.com | Czech Republic | | Helena | Holý | hholy@gmail.com | Czech Republic | | Astrid | Gruber | astrid.gruber@apple.at | Austria | | Daan | Peeters | daan_peeters@apple.be | Belgium | | Kara | Nielsen | kara.nielsen@jubii.dk | Denmark | | Eduardo | Martins | eduardo@woodstock.com.br | Brazil | | Alexandre | Rocha | alero@uol.com.br | Brazil | | Roberto | Almeida | roberto.almeida@riotur.gov.br | Brazil | | Fernanda | Ramos | fernadaramos4@uol.com.br | Brazil | | João | Fernandes | jfernandes@yahoo.pt | Portugal | | Madalena | Sampaio | masampaio@sapo.pt | Portugal | | Hannah | Schneider | hannah.schneider@yahoo.de | Germany | | Fynn | Zimmermann | fzimmermann@yahoo.de | Germany | | Niklas | Schröder | nschroder@surfeu.de | Germany | | Camille | Bernard | camille.bernard@yahoo.fr | France | | Dominique | Lefebvre | dominiquelefebvre@gmail.com | France | | Marc | Dubois | marc.dubois@hotmail.com | France | | Wyatt | Girard | wyatt.girard@yahoo.fr | France | | Isabelle | Mercier | isabelle_mercier@apple.fr | France | | Terhi | Hämäläinen | terhi.hamalainen@apple.fi | Finland | | Ladislav | Kovács | ladislav_kovacs@apple.hu | Hungary | | Hugh | O'Reilly | hughoreilly@apple.ie | Ireland | +-----------+-------------+-------------------------------+----------------+ (Output limit exceeded, 25 of 38 total rows shown)

Key takeaways  關鍵要點

Logical operators allow you to create more specific filters that target the security-related information you need. The AND operator requires two conditions to be true simultaneously, the OR operator requires either one or both conditions to be true, and the NOT operator negates a condition. Logical operators can be combined together to create even more specific queries.
邏輯運算子允許您創建更具針對性的篩選器,以鎖定您所需的安全相關資訊。AND 運算子要求兩個條件同時為真,OR 運算子要求其中一個或兩個條件為真,而 NOT 運算子則否定一個條件。邏輯運算子可以結合在一起,以創建更具針對性的查詢。