📅  最后修改于: 2020-11-18 01:56:03             🧑  作者: Mango
MySQL中的EXISTS运算符是一种布尔运算符,它返回true或false。它与子查询结合使用,并检查子查询中数据的存在。这意味着如果子查询返回任何记录,则此运算符将返回true。否则,它将返回false。真值始终表示数字值1,假值始终表示0。我们可以将其与SELECT,UPDATE,DELETE,INSERT语句一起使用。
以下是在MySQL中使用EXISTS运算符的语法:
SELECT col_names
FROM tab_name
WHERE [NOT] EXISTS (
SELECT col_names
FROM tab_name
WHERE condition
);
NOT运算符用于否定EXISTS运算符。当子查询不返回任何行时,它返回true。否则,它返回false。
通常,EXISTS查询以SELECT *开头,但可以以SELECT列,SELECT a_constant或子查询中的任何内容开头。由于MySQL会忽略SUBQUERY中的选择列表,因此它将提供相同的输出。
找到匹配结果后,此运算符符立即终止以进行进一步处理。此功能提高了MySQL查询的性能。
以下是EXISTS运算符使用的参数:
Parameter Name | Descriptions |
---|---|
col_names | It is the name of column(s) that contains in the specified table. |
tab_name | It is the name of the table from which we are going to perform the EXISTS operator. |
condition | It specifies for searching the specific value from the table. |
subquery | It is usually the SELECT statement that begins with SELECT *, but MySQL ignores it in a subquery. |
让我们了解EXISTS运算符在MySQL中的工作方式。在这里,我们将首先使用以下语句创建两个名为“ customer”和“ orders”的表:
CREATE TABLE customer(
cust_id int NOT NULL,
name varchar(35),
occupation varchar(25),
age int
);
CREATE TABLE orders (
order_id int NOT NULL,
cust_id int,
prod_name varchar(45),
order_date date
);
接下来,我们需要在两个表中插入值。执行以下语句:
INSERT INTO customer(cust_id, name, occupation, age)
VALUES (101, 'Peter', 'Engineer', 32),
(102, 'Joseph', 'Developer', 30),
(103, 'John', 'Leader', 28),
(104, 'Stephen', 'Scientist', 45),
(105, 'Suzi', 'Carpenter', 26),
(106, 'Bob', 'Actor', 25),
(107, NULL, NULL, NULL);
INSERT INTO orders (order_id, cust_id, prod_name, order_date)
VALUES (1, '101', 'Laptop', '2020-01-10'),
(2, '103', 'Desktop', '2020-02-12'),
(3, '106', 'Iphone', '2020-02-15'),
(4, '104', 'Mobile', '2020-03-05'),
(5, '102', 'TV', '2020-03-20');
要验证表,请运行SELECT命令,如下所示:
msql> SELECT * FROM customer;
AND,
mysql> SELECT * FROM orders;
我们将获得以下输出:
在此示例中,我们将使用EXISTS运算符查找已下达至少一个订单的客户的姓名和职业:
mysql> SELECT name, occupation FROM customer
WHERE EXISTS (SELECT * FROM Orders
WHERE customer.cust_id = Orders.cust_id);
出现以下输出:
同样,如果我们想获取尚未下订单的客户的名称,请使用NOT EXISTS运算符:
mysql> SELECT name, occupation FROM customer
WHERE NOT EXISTS (SELECT * FROM Orders
WHERE customer.cust_id = Orders.cust_id);
它将给出以下输出:
假设我们要从Orders表中删除一条order_id = 3的记录,请执行以下查询,以从Orders表中永久删除该记录:
mysql> DELETE FROM Orders WHERE EXISTS (
SELECT * FROM customer
WHERE order_id=3);
要验证输出,请运行以下命令:
mysql> SELECT * FROM Orders;
在输出中,我们可以看到成功删除了order_id = 3的表记录。
如果要检查表中是否存在行,请使用以下查询:
mysql> SELECT EXISTS(SELECT * from customer WHERE cust_id=104) AS Result;
我们将获得表示真实的输出1。因此,表中存在cust_id = 104。
EXISTS和IN运算符之间的主要区别以表格形式给出:
SN | IN | EXISTS |
---|---|---|
1. | It is used to minimize the multiple OR conditions in MySQL. | It is used to check the existence of data in a subquery. |
2. | SELECT col_names FROM tab_name WHERE col_name IN (subquery); |
SELECT col_names FROM tab_name WHERE [NOT] EXISTS (subquery); |
3. | It compares all values inside the IN operator. | It stops for further execution as soon as it finds the first true occurrence. |
4. | It can use for comparing NULL values. | It cannot use for comparing NULL values. |
5. | It executes faster when the subquery result is less. | It executes faster when the subquery result is large. |
6. | It performs a comparison between parent query and child query or subquery. | It does not perform a comparison between parent query and child query or subquery. |