Mysql 简明教程

MySQL - ANY Operator

MySQL 中的操作符与数学中的操作符具有相同的含义。它们是 MySQL 语句中用于执行比较或逻辑运算的关键字。

The operators in MySQL have the same meaning as that of operators in mathematics. They are keywords that are used in MySQL statements for performing comparisons or logical operations.

ANY Operator in MySQL

MySQL ANY 关键字可与比较运算符(如 =、<、>、⇐、>=、<>)配合使用,以将值与子查询返回的值集进行比较。

The MySQL ANY keyword can be used with a comparison operator (such as =, <, >, ⇐, >=, <>) to compare a value with a set of values returned by the subquery.

  1. This operator will return true if the given condition is satisfied for any of the values in the set.

  2. This operator will return false if none of the values in the specified set satisfy the given condition.

ANY 运算符必须以标准比较运算符(即 >、>=、<、⇐、=、<>、!=)开头,后面跟一个子查询。

The ANY operator must be preceded by a standard comparison operator i.e. >, >=, <, ⇐, =, <>, !=, and followed by a subquery.

Syntax

下面是 MySQL 中 ANY 运算符的语法 −

Following is the syntax of the ANY operator in MySQL −

SELECT column_name1, column_name2, ...
FROM table_name
WHERE column_name operator ANY (subquery);

其中,

Where,

  1. column_name is the name of a column to be compared with a subquery.

  2. operator is a comparison operator such as =, <, >, ⇐, >=, or <>.

  3. subquery is a SELECT statement that returns a single column of values.

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)
);

以下查询向上述创建的 MySQL 表中插入 7 条记录 −

The following query inserts 7 records into the above-created MySQL 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 below query to retrieve all the records present in the CUSTOMERS table −

SELECT * FROM CUSTOMERS;

以下为 CUSTOMERS 表 −

Following is the CUSTOMERS table −

ANY with ">" Operator

MySQL ANY 运算符可与比较运算符“>”(大于)配合使用,以验证特定列值是否大于子查询返回的其他记录的列值。

The MySQL ANY operator can be used with the comparison operator ">" (greater than) to verify whether a particular column value is greater than the column value of any of the other records returned by the subquery.

Example

在以下查询中,我们正在从 CUSTOMERS 表中选择所有记录,其中 SALARY 列大于与年龄为 22 的客户关联的任何薪水。

In the following query, we are selecting all records from the CUSTOMERS table where the SALARY column is greater than any of the salaries associated with customers whose age is 22.

SELECT * FROM CUSTOMERS
WHERE SALARY > ANY
(SELECT SALARY FROM CUSTOMERS WHERE AGE = 22);

Output

年龄为 22 的客户的薪水为 4500。以下是薪水高于 4500 的客户(年龄=22)。

The salary of customer with age 22 is 4500. The following are the customers whose salaries are greater than 4500 (age=22).

ANY with "<" Operator

我们可以将比较运算符 "<" (小于)与 MySQL ANY 运算符一起使用,以验证特定列值是否小于子查询返回的任何记录的列值。

We can use the comparison operator "<" (less than) with the MySQL ANY operator to verify whether a particular column value is less than the column value of any of the records returned by the subquery.

Example

在此查询中,我们正在从 CUSTOMERS 表中选择所有记录,其中 SALARY 列小于与年龄为 32 的客户关联的任何薪水。

In this query, we are selecting all records from the CUSTOMERS table where the SALARY column is less than any of the salaries associated with customers whose age is 32.

SELECT * FROM CUSTOMERS
WHERE SALARY < ANY
(SELECT SALARY FROM CUSTOMERS WHERE AGE = 32);

Output

32 岁客户的薪水为 2000。唯一薪水低于 2000 的客户是 'Khilan' −

The salary of 32 aged customer is 2000. The only customer with salary less than 2000 is 'Khilan' −

ANY with "=" operator

我们可以将 MySQL ANY 运算符与比较运算符 "=" (等于)一起使用,以从表中提取列值等于子查询返回的任何值的记录。

We can use the MySQL ANY operator with the comparison operator "=" (equal to) to fetch the records from a table where a column value is equal to any value returned by a subquery.

Example

在这里,我们正在尝试从 CUSTOMERS 表中选择所有记录,其中 AGE 列与名为“Khilan”的客户关联的任何 AGE 匹配。

Here, we are trying to select all records from the CUSTOMERS table where the AGE column matches any of the AGE associated with customers named "Khilan".

SELECT * FROM CUSTOMERS
WHERE AGE = ANY
(SELECT AGE FROM CUSTOMERS WHERE NAME = "Khilan");

Output

'khilan' 的年龄是 25 岁。另一个年龄等于 25 岁的客户是 'Chaitali'。

The age of 'khilan' is 25. Another customer whose age is equal to 25 is 'Chaitali'.

ANY with "<>" Operator

我们可以将 MySQL ANY 运算符与 "<>" (不等于)比较运算符一起使用,以从表中提取列值不等于子查询返回的任何值的记录。

We can use the MySQL ANY operator with "<>" (not equal to) comparison operator to fetch the records from a table where a column value is not equal to any value returned by a subquery.

Example

在此查询中,我们在 CUSTOMERS 表中选择地址列与名为 "Ramesh" 的客户关联的任何地址都不匹配的所有记录。

In this query, we are selecting all records from the CUSTOMERS table where the ADDRESS column does not match any of the addresses associated with customers named "Ramesh".

SELECT * FROM CUSTOMERS
WHERE ADDRESS <> ANY
(SELECT ADDRESS FROM CUSTOMERS WHERE NAME = "Ramesh");

Output

'Ramesh' 的地址是艾哈迈达巴德。以下客户的地址不等于艾哈迈达巴德。

The address of 'Ramesh' is Ahmedabad. Following are the customers whose address is not equal to Ahmedabad.

ANY with "⇐" Operator

MySQL ANY 运算符在与 "⇐" 比较运算符一起使用时,如果值小于等于指定集合中的任何值,则返回 true。

The MySQL ANY operator returns true if a value is less than or equal to any value in a specified set when used with the "⇐" comparison operator.

Example

在此,我们在 CUSTOMERS 表中选择 AGE 列小于等于 AGE 列中任何年龄值的所有记录,其中 SALARY 等于 10000。

Here, we are selecting all records from the CUSTOMERS table where the AGE column is less than or equal to any age value in the AGE column where the SALARY is equal to 10000.

SELECT * FROM CUSTOMERS
WHERE AGE <= ANY (SELECT AGE FROM CUSTOMERS WHERE SALARY = 10000);

output

薪水为 10000 的客户年龄为 24 岁。因此,年龄小于 24 岁的客户如下 −

The age of customer whose salary is 10000 is 24. So, the following are the customers whose age is less than 24 −

ANY with ">=" Operator

MySQL ANY 运算符在与 ">=" 比较运算符一起使用时,如果值大于等于指定集合中的任何值,则返回 true。

The MySQL ANY operator returns true if a value is greater than or equal to any value in a specified set when used with the ">=" comparison operator.

Example

在此查询中,我们在 CUSTOMERS 表中选择所有记录,其中 "AGE" 大于或等于从 "CUSTOMERS" 中通过选择 "AGE"(其中 "SALARY" 等于 10,000)获得的结果集中的任何值。

In this query, we are selecting all records from the CUSTOMERS table where the 'AGE' is greater than or equal to any value in the result set obtained by selecting 'AGE' from 'CUSTOMERS' where 'SALARY' is equal to 10,000.

SELECT * FROM CUSTOMERS
WHERE AGE >= ANY
(SELECT AGE FROM CUSTOMERS WHERE SALARY = 10000);

Output

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

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

ANY Operator Using a Client Program

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

Besides using MySQL queries to perform the ANY 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 −