A brief analysis of the difference between and and where in MySQL connection query

A brief analysis of the difference between and and where in MySQL connection query

1. Create a table

CREATE TABLE `student` (
  `id` int(11) NOT NULL,
  `name` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL,
  `age` int(11) NULL DEFAULT NULL,
  PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci ROW_FORMAT = Dynamic;


INSERT INTO `student` VALUES (1, '张三', 12);
INSERT INTO `student` VALUES (2, '李四', 12);
INSERT INTO `student` VALUES (3, '王五', 12);
INSERT INTO `student` VALUES (4, '赵六', 12);
INSERT INTO `student` VALUES (5, '孙七', 12);
INSERT INTO `student` VALUES (6, 'turtle', 12);
CREATE TABLE `grade` (
  `id` int(11) NOT NULL,
  `sid` int(11) NULL DEFAULT NULL,
  `grade` int(11) NULL DEFAULT NULL,
  PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci ROW_FORMAT = Dynamic;

INSERT INTO `grade` VALUES (1, 1, 100);
INSERT INTO `grade` VALUES (2, 2, 80);
INSERT INTO `grade` VALUES (3, 3, 99);
INSERT INTO `grade` VALUES (4, 4, 66);

2. Inner join

There is no difference between and and where in inner joins. Both use the results after the join to perform conditional screening.

2.1 No conditions

2.2 and Condition

2.3 where condition

3. left join

3.1 No conditions

3.2 and Condition

In left join, the left table is fully matched for connection, and then AND is used for filtering; the left table data that does not meet the conditions is retained, and the right table data is null.

3.3 where condition

In left join, the left table is fully matched for connection, and then the where clause is used for filtering; only the data that meets the conditions is filtered.

4. right join

Same as left join in 3, except that the base tables are reversed.

5. Conclusion

  • For all connections, use where to filter the data rows that meet the conditions after the connection, and only keep the data rows that meet the conditions;
  • When left join is used, and the left table is used as the main table. All data in the left table is retained, and the data rows that do not meet the conditions are set to null in the right table.
  • When right join is used, and the right table is used as the main table. All data in the right table is retained, and the left table data that does not meet the conditions is null;

This is the end of this article about the difference between and and where in MySQL connection query. For more relevant MySQL connection query and where content, please search 123WORDPRESS.COM's previous articles or continue to browse the following related articles. I hope everyone will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • Mysql multi-condition query statement with And keyword
  • MySQL conditional query and or usage and priority example analysis
  • Priority analysis of and or queries in MySQL
  • MySQL left-join multi-table query where condition writing example
  • An example of how PHP converts MySQL query results into an array and concatenates them with where
  • Analysis of the difference between placing on and where in MySQL query conditions

<<:  Summary of Docker Consul container service updates and issues found

>>:  Introduction to CSS style introduction methods and their advantages and disadvantages

Recommend

Vue/react single page application back without refresh solution

Table of contents introduction Why bother? Commun...

How to change the CentOS server time to Beijing time

1. I purchased a VPS and CentOS system, and found...

Simple example of limit parameter of mysql paging

Two parameters of Mysql paging select * from user...

How to understand JS function anti-shake and function throttling

Table of contents Overview 1. Function debounce 2...

Ubuntu20.04 VNC installation and configuration implementation

VNC is a remote desktop protocol. Follow the inst...

10 content-related principles to improve website performance

<br />English address: http://developer.yaho...

Gradient slide effect implemented by CSS3

Achieve results Code html <div class="css...

Vue local component data sharing Vue.observable() usage

As components become more detailed, you will enco...

Website background music implementation method

For individual webmasters, how to make their websi...

CSS complete parallax scrolling effect

1. What is Parallax scrolling refers to the movem...

Analysis of Linux Zabbix custom monitoring and alarm implementation process

Target Display one of the data in the iostat comm...

Solution to docker suddenly not being accessible from the external network

According to the methods of the masters, the caus...

Why Google and Facebook don't use Docker

The reason for writing this article is that I wan...

Detailed explanation of mandatory and implicit conversion of types in JavaScript

Table of contents 1. Implicit conversion Conversi...