|
The OR operator is a logical operator that combines two Boolean expressions or predicates.
The OR operator is often used in the WHERE clause of the SELECT, UPDATE, and DELETE statements to specify a search condition for rows to be selected, updated,
and deleted.
Here is the syntax of the OR operator:
O operador OR é um operador lógico que combina duas expressões ou predicados booleanos.
O operador OR é frequentemente utilizado na cláusula WHERE dos SELECT, UPDATE e declarações DELETE para especificar uma condição de pesquisa para as linhas a serem
selecionados, atualizados e excluídos.
Esta é a sintaxe do operador OR:
SELECT column1, column2, ...
FROM table_name
WHERE condition1 OR condition2 OR condition3 ...;
|
This table displays the result when combining true, false, and unknown values using the AND operator:
Esta tabela exibe o resultado ao combinar valores verdadeiro, falso e desconhecido usando o ANDoperador:
| |
TRUE - VERDADEIRO |
FALSE - FALSO |
UNKNOWN - DESCONHECIDO |
| TRUE - VERDADEIRO |
TRUE - VERDADEIRO |
TRUE - VERDADEIRO |
TRUE - VERDADEIRO |
| FALSE - FALSO |
TRUE - VERDADEIRO |
FALSE - FALSO |
UNKNOWN - DESCONHECIDO |
| UNKNOWN - DESCONHECIDO |
TRUE - VERDADEIRO |
UNKNOWN - DESCONHECIDO |
UNKNOWN - DESCONHECIDO |
If you use both OR and AND operators in an expression, Db2 always evaluates the AND operator first.
To change the order of evaluation, you can use the parentheses.
To negate the OR operator, you use the NOT operator as follows:
Se você usar os operadores OR e AND em uma expressão, o Db2 sempre avalia o operador AND primeiro.
Para alterar a ordem de avaliação, você pode usar os parênteses.
Para negar o operador OR, você usa o operador NOT da seguinte maneira:
SELECT column1, column2, ...
FROM table_name
WHERE NOT (condition1 OR condition2 OR condition3 ...;)
|
DB2 Database - Banco de dados DB2:
Below is a selection from the "Product" table in the DB2 database.
Abaixo está uma seleção da tabela "Produto" no banco de dados DB2.
| ProductId | ProductName | Category | Price | QtyOnHand | TotalValue |
| 7001 | Mouse | Accessories | 75.00 | | |
| 7002 | Harddrive | | 65.00 | 20 | 1,300 |
| 7003 | Keyboard | Accessories | 36.00 | 33 | 1,118.00 |
| 7004 | Ram | Components | 23.50 | 16 | 376.00 |
| 7005 | Honda | Bikes | 1,200 | | |
| 7006 | Pen | | 7.45 | 10 | 74.50 |
| 7007 | Mousepad | Accessories | 5.00 | | |
The OR operator is exactly the opposite of AND.
O operador OR é exatamente o oposto de AND.
The OR operator instructs the DB2 to retrieve rows that match either one condition or both.
O operador OR instrui o DB2 a recuperar linhas que correspondam a uma das condições ou a ambas.
SELECT ProductId, ProductName, Price
FROM Product
WHERE Category = 'Accessories' OR Price > 10;
|
This SQL statement retrieves the ProductId, ProductName and Price for any products of either the Category = 'Accessories' or Price > 10
Esta instrução SQL recupera ProductId, ProductName e Price para qualquer produto de Category = 'Accessories' ou Price> 10
Result - Resultado:
ProductId ProductName Price
7001 Mouse 75.00
7003 Keyboard 36.00
7007 Mousepad 5.00
|
Order of Evaluation - Ordem de avaliação:
WHERE clauses can contain any number of AND and OR operators.
As cláusulas WHERE podem conter qualquer número de operadores AND e OR.
SQL processes AND operators before OR operators.
SQL processa operadores AND antes dos operadores OR.
Example - Exemplo:
To get a list of all ProductId for the Category 'Accessories' and 'Components' with Price 10 or more.
Para obter uma lista de todos os ProductId da categoria 'Acessórios' e 'Componentes' com preço 10 ou mais.
SELECT ProductId
FROM Product
WHERE (Category = 'Accessories' OR Category = 'Components') AND Price >= 10;
|
If you do not use parentheses, you will not get desired output.
Se você não usar parênteses, não obterá a saída desejada.
Whenever you write WHERE clauses that use both AND and OR operators, use parentheses to explicitly group operators.
Sempre que você escrever cláusulas WHERE que usam os operadores AND e OR, use parênteses para agrupar explicitamente os operadores.
Result - Resultado:
|