Use of mysql EXISTS

The format used is as follows:

select * from a  where exists (any subquery)

The code is divided into two sections according to the color. The first one is the main query, and the red one is the subquery . The main query is first and then the subquery. The attributes in the main query can be used in the subquery.

Usage meaning:

If the subquery "has data results", as long as the query returns the number of result rows, the result of the exists() is "true"

If the subquery "has no data result" and the query does not return the number of result rows, the result of the exists() is "false"

What do you mean by data results here ?

1. There are data results

(1) Query all data: SELECT * FROM student

(2) Query the total number of data: SELECT COUNT(1) FROM student

(3) Query a non-existent data, display the number: SELECT COUNT(1) FROM student WHERE id=888;

2. No data results

Query data records that do not exist: SELECT * FROM student WHERE id=888;

As above, as long as there are data results returned, the result of exists() is "true", otherwise it is false.

Use demo to demonstrate the usage of where exists

(1) The table construction statement is as follows

SET FOREIGN_KEY_CHECKS=0;

-- ----------------------------
-- Table structure for student
-- ----------------------------
DROP TABLE IF EXISTS `student`;
CREATE TABLE `student` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(255) DEFAULT NULL COMMENT '名字',
  `age` int(11) DEFAULT NULL COMMENT '年龄',
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=latin1;

-- ----------------------------
-- Records of student
-- ----------------------------
INSERT INTO `student` VALUES ('1', 'zhangsan', '18');
INSERT INTO `student` VALUES ('2', 'lisi', '19');
INSERT INTO `student` VALUES ('3', 'wangwu', '20');

(2) The results of table building are as follows

student table

Prerequisite: There is a record with id=1, and there is no record with id=111.

(3) All three of these return all records, because the where exists subquery has data results, so it returns true

SELECT * FROM student st WHERE EXISTS (SELECT * FROM student);
SELECT * FROM student st WHERE EXISTS (SELECT 1 FROM student);
SELECT * FROM student st WHERE EXISTS (SELECT COUNT(1) FROM student);

Results one two three:

(4) Of these two, the upper one does not return data, the lower one returns all data

SELECT * FROM student st WHERE EXISTS (SELECT 1 FROM student dd where dd.id=111);
SELECT * FROM student st WHERE EXISTS (SELECT COUNT(1) FROM student dd where dd.id=111);

Result one does not return data:

Result two returns data:

(5) The following four

SELECT * FROM student st WHERE EXISTS (SELECT 1 FROM student dd where dd.id=1);
SELECT * FROM student st WHERE EXISTS (SELECT 1 FROM student dd where dd.id=111);
SELECT * FROM student st WHERE EXISTS (SELECT 1 FROM student dd where st.id=1);
SELECT * FROM student st WHERE EXISTS (SELECT 1 FROM student dd where st.id=111);

Result one:

Result 2:

Result three:

Result 4:

Summary: (1) The overall query is divided into the main sub-query, the main query is in front, the sub-query is followed by the where exists, the main is the sub-query first.

EXPLAIN SELECT * FROM student st WHERE EXISTS (SELECT 1 FROM student dd where dd.id=1);

           (2) In the subquery, if there is a result, exists returns true, if there is no result, returns false. The exists subquery returns true, the main query returns all content normally, the exists subquery returns false, and the main query returns nothing.

           (3) The subquery count(1) always has results. Even if count(1) returns 0, there are results returned, so where exists uses count(1) to always return true.

           (4) The exists subquery uses select 1. If there is a return result, the subquery returns true, and if no result is returned, the subquery returns false.

           (5) Use select *, select 1, select count(1) for a subquery. The content of the result returned by the subquery is not important. As long as there is a result, it returns true, and if there is no result, it returns false.

           (6) Subqueries can use the fields of the main query. as follows:

The first statement does not use the fields of the main query, you just need to determine whether WHERE EXISTS has a result returned.

SELECT * FROM student st WHERE EXISTS (SELECT 1 FROM student dd where dd.id=1);

The second statement st.id=1 uses the st table field of the main query, which will limit the content of the main query.

SELECT * FROM student st WHERE EXISTS (SELECT 1 FROM student dd where st.id=1);

This statement is equivalent to

SELECT * FROM student st where st.id=1;

Guess you like

Origin blog.csdn.net/Mint6/article/details/105084644