Mysql 简明教程

MySQL - OR Operator

MySQL OR Operator

MySQL 没有内置的布尔数据类型。相反,布尔值使用数字数据类型表示,其中零用作 false ,任何非零值用作 true

MySQL does not have a built-in Boolean data type. Instead, Boolean values are represented using numeric data types, where zero is used as false and any non-zero value is used as true.

MySQL OR 运算符是一种逻辑运算符,它结合两个或多个布尔表达式并返回 1、0 或 NULL:

The MySQL OR operator is a logical operator that combines two or more Boolean expressions and returns 1, 0, or NULL:

A AND B

此处, AB 是运算数。

Here, A and B are operands.

  1. The OR operator will return true (1) only if either A or B, or both, is non-zero and not Null.

  2. If both A and B are false, the OR operator will return false (0).

  3. If either A or B is NULL, the OR operator will return NULL.

下表演示了使用 OR 运算符组合真 (1)、假 (0) 和空值可能的输出:

The following table below demonstrates the possible outcomes of using the OR operator to combine true (1), false (0), and null values:

Example

如果 A 和 B 均不为空,且 A 或 B 任一为非零,则逻辑 OR 运算符将返回真 (1)。

The logical OR operator will return true (1) if both A and B are not NULL, and if either A or B is non-zero.

SELECT 1 OR 1, 1 OR 0, 0 OR 1;

Output

以上程序的输出如下所示:

The output for the program above is produced as given below −

Example

如果 A 和 B 均为假 (0),则 OR 运算符返回假 (0)。

The OR operator returns false (0) if both A and B are false (0).

SELECT 0 OR 0;

Output

当我们执行上述查询时,输出将获得如下:

When we execute the above query, the output is obtained as follows −

Example

如果 A 为真 (1) 而 B 为空值,则 OR 运算符将返回 1。

If A is true (1) and B is NULL, the OR operator will return 1.

如果 A 为假 (0) 而 B 为空值,则 OR 运算符将返回 NULL。

If A is false (0) and B is NULL, the OR operator will return NULL.

如果 A 和 B 均为空值,则 OR 运算符将返回 NULL。

If both A and B are NULL, the OR operator will return NULL.

SELECT 1 OR NULL, 0 OR NULL, NULL or NULL;

Output

执行给定的程序后,输出如下所示:

On executing the given program, the output is displayed as follows −

OR operator with WHERE

MySQL 的逻辑 OR 运算符可与 WHERE 子句一起使用,以返回满足任何指定条件的行。

MySQL’s logical OR operator can be used along with the WHERE clause to return the rows that meet any of the specified conditions.

当使用 OR 运算符时,必须至少满足一个条件,行才能包含在结果集中。如果没有任何条件为真,则返回空集。

When the OR operator is used, at least one of the conditions must be true for a row to be included in the result set. If none of the conditions are true, an empty set is returned.

Syntax

以下是在 MySQL 中带有 WHERE 子句的 OR 运算符的语法:

Following is the syntax of the OR operator with WHERE clause in MySQL −

SELECT column1, column2, ..., columnN
FROM table_name
[WHERE condition1 OR condition2 OR condition3 ...;]

Example

首先,让我们使用以下查询创建一个名为 CUSTOMERS 的表 −

Firstly, let us create a table named CUSTOMERS using the following query −

CREATE TABLE CUSTOMERS (
   ID INT AUTO_INCREMENT,
   NAME VARCHAR(20) NOT NULL,
   AGE INT NOT NULL,
   ADDRESS CHAR (25),
   SALARY DECIMAL (18, 2),
   PRIMARY KEY (ID)
);

以下 INSERT INTO 语句将 7 条记录添加到上面创建的表中:

The following INSERT INTO statement adds 7 records into the above-created table −

INSERT INTO CUSTOMERS (ID,NAME,AGE,ADDRESS,SALARY) VALUES
(1, 'Ramesh', 32, 'Ahmedabad', 2000.00 ),
(2, 'Khilan', 25, 'Delhi', 1500.00 ),
(3, 'Kaushik', 23, 'Kota', 2000.00 ),
(4, 'Chaitali', 25, 'Mumbai', 6500.00 ),
(5, 'Hardik', 27, 'Bhopal', 8500.00 ),
(6, 'Komal', 22, 'Hyderabad', 4500.00 ),
(7, 'Muffy', 24, 'Indore', 10000.00 );

执行以下查询以检索 CUSTOMERS 表中存在的所有记录 −

Execute the following query to retrieve all the records present in the CUSTOMERS table −

SELECT * FROM CUSTOMERS;

以下为 CUSTOMERS 表 −

Following is the CUSTOMERS table −

现在,我们正在从 CUSTOMERS 表中选择 SALARY 大于 5000 或 ADDRESS =“Hyderabad”的所有列。

Now, we are selecting all the columns from the CUSTOMERS table where SALARY is greater than 5000 or ADDRESS = "Hyderabad".

SELECT * FROM CUSTOMERS
WHERE SALARY > 5000
OR ADDRESS = "Hyderabad";

Output

以上程序的输出如下所示:

The output for the program above is produced as given below −

Example

只有当 OR 分隔的条件任一为真的情况下,逻辑 OR 运算符才会返回记录。

The logical OR operator returns the records only if either of the conditions separated by OR is true.

在以下查询中,我们正在为 OR 运算符的两个运算数提供假值。

In the following query, we are providing false values to both operands of the OR operator.

SELECT * FROM CUSTOMERS
WHERE NAME = "Mahesh" OR AGE = 42;

Output

由于在CUSTOMERS表中没有名称为“Mahesh”或年龄为42的记录,จึง返回一个空集合作为输出。

As there are no records present in the CUSTOMERS table with NAME "Mahesh" or AGE is 42, it returns an empty set as an output.

Empty set (0.00 sec)

Multiple OR Operators

我们可以多次使用MySQL的逻辑OR运算符组合多个条件。通过使用多个OR运算符,满足至少一个条件的任何行都将包含在结果集中。

We can use MySQL’s logical OR operator multiple times to combine multiple conditions. By using multiple OR operators, any rows that meet at least one of the conditions will be included in the result set.

Example

在以下查询中,我们将返回CUSTOMERS表的客户名称以“k”结尾的所有记录,或SALARY大于5000,或AGE小于25的所有记录。

In the following query, we are returning all the records from the CUSTOMERS table where the NAME of the customer ends with 'k', or SALARY is greater than 5000, or AGE is less than 25.

SELECT * FROM CUSTOMERS
WHERE NAME LIKE '%k' OR SALARY > 5000 OR AGE < 25;

Output

执行给定的查询后,输出如下:

On executing the given query, the output is displayed as follows −

OR with UPDATE statement

MySQL的逻辑OR运算符可以与 UPDATE 一起使用,基于多个条件更新表的记录。

The MySQL’s logical OR operator can be used along with the UPDATE statement to update records of a table based on multiple conditions.

Syntax

以下是MySQL中UPDATE语句与OR运算符的语法——

Following is the syntax of the OR operator with the UPDATE statement in MySQL −

UPDATE table_name
SET column1 = value1, column2 = value2, ...
WHERE condition1 OR condition2 OR ...;

Example

在以下查询中,我们正在更新ADDRESS为“Hyderabad”或年龄大于26的CUSTOMERS的SALARY

In the following query, we are updating the SALARY of CUSTOMERS whose ADDRESS is 'Hyderabad' or whose age is greater than 26

UPDATE CUSTOMERS
SET SALARY = 15000
WHERE ADDRESS = "Hyderabad" OR AGE > 26;

Output

以上查询的输出如下所示:

The output for the query above is produced as given below −

Query OK, 3 rows affected (0.00 sec)
Rows matched: 3  Changed: 3  Warnings: 0

Verification

执行以下查询以验证CUSTOMERS的SALARY是否更新——

Execute the below query to verify whether the SALARY of CUSTOMERS is updated or not −

SELECT * FROM CUSTOMERS;

Output

以上程序的输出如下所示:

The output for the program above is produced as given below −

OR with DELETE Statement

MySQL的逻辑OR运算符可以与 DELETE 一起使用,基于多个条件从表中删除记录。

The MySQL’s logical OR operator can be used along with the DELETE statement to remove records from a table based on multiple conditions.

Syntax

以下是MySQL中DELETE语句与OR运算符的语法——

Following is the syntax of OR operator with the DELETE statement in MySQL −

DELETE FROM table_name
WHERE condition1 OR condition2 OR condition3 ...

Example

在以下查询中,我们尝试DELETE CUSTOMERS表中年龄小于25岁或SALARY小于或等于10000的记录。

In the following query, we are trying to DELETE records from the CUSTOMERS table where the age is less than 25 or the SALARY is less than or equal to 10000.

DELETE FROM CUSTOMERS
WHERE AGE < 25 OR SALARY <= 10000;

Output

Query OK, 5 rows affected (0.01 sec)

Verification

执行以下查询以验证上述操作是否成功——

Execute the following query to verify whether the above operation is successful or not −

OR Operator Using a Client Program

除了使用MySQL查询来执行OR运算符之外,我们还可以使用像Node.js、PHP、Java和Python这样的客户端程序来实现相同的结果。

Besides using MySQL queries to perform the OR operator, we can also use client programs like Node.js, PHP, Java, and Python to achieve the same result.

Syntax

以下是此操作在各种编程语言中的语法 −

Following are the syntaxes of this operation in various programming languages −

Example

以下是这些程序 −

Following are the programs −