Priority analysis of and or queries in MySQL

Priority analysis of and or queries in MySQL

This may be an issue that is easily overlooked. First of all, we must be clear:
In MySQL, AND has a higher execution priority than OR. That is to say, without the restriction of parentheses (), AND statements are always executed first, and then OR statements.
for example:

select * from table where condition 1 AND condition 2 OR condition 3
Equivalent to select * from table where (condition 1 AND condition 2) OR condition 3

select * from table where condition 1 AND condition 2 OR condition 3 AND condition 4
Equivalent to select * from table where (condition 1 AND condition 2) OR (condition 3 AND condition 4)

Let’s take a look at some examples to understand more deeply:

Test table data:

SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;

-- ----------------------------
-- Table structure for book
-- ----------------------------
DROP TABLE IF EXISTS `book`;
CREATE TABLE `book` (
 `id` int(10) UNSIGNED NOT NULL AUTO_INCREMENT,
 `name` varchar(25) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci DEFAULT NULL,
 `author` varchar(25) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci DEFAULT NULL,
 `price` decimal(10, 2) DEFAULT NULL,
 PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB CHARACTER SET = utf8mb4 COLLATE = utf8mb4_0900_ai_ci ROW_FORMAT = Dynamic;

-- ----------------------------
-- Records of book
-- ----------------------------
INSERT INTO `book` VALUES (1, 'PHP', 'mate', 21.00);
INSERT INTO `book` VALUES (2, 'JAVA', 'kaven', 23.00);
INSERT INTO `book` VALUES (3, 'JAVA Advanced', 'loose', 45.00);
INSERT INTO `book` VALUES (4, 'GO', 'jim', 46.00);
INSERT INTO `book` VALUES (5, 'GO Design', 'json', 76.00);
INSERT INTO `book` VALUES (6, 'PHP Advanced Programming', 'bate', 67.00);
INSERT INTO `book` VALUES (7, 'Python', 'jim', 66.00);
INSERT INTO `book` VALUES (8, 'Python Design', 'mali', 54.00);
INSERT INTO `book` VALUES (9, 'GO Programming', 'kaven', 86.00);
INSERT INTO `book` VALUES (11, 'Python3', 'jim', 55.00);

SET FOREIGN_KEY_CHECKS = 1;

Query method 1:

SELECT * FROM book WHERE author='jim' OR author='json' AND name='PHP';

The above query is equivalent to:

SELECT * FROM book WHERE author='jim' OR (author='json' AND name='PHP');

Then the above query results are easy to understand.

Query method 2:

SELECT * FROM book WHERE name='PHP' AND author='jim' OR author='json';

The above query is equivalent to:

SELECT * FROM book WHERE (name='PHP' AND author='jim') OR author='json';

Query method 3:

SELECT * FROM book WHERE name='GO' AND (author='jim' OR author='json');

This is easy to understand. Understand the precedence of and or. These queries are not so "confusing to understand".

This is the end of this article about the priority analysis of and or queries in MySQL. For more relevant MySQL and or query content, please search for previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope you 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
  • 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
  • A brief analysis of the difference between and and where in MySQL connection query

<<:  abbr mark and acronym mark

>>:  The problem of form elements and prompt text not being aligned

Recommend

Website background music implementation method

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

Five solutions to cross-browser problems (summary)

Brief review: Browser compatibility issues are of...

How to use translate and transition in CSS3

I always feel that translate and transition are v...

How to expand the disk space of Linux server

Table of contents Preface step Preface Today I fo...

Mysql practical exercises simple library management system

Table of contents 1. Sorting function 2. Prepare ...

How to use Element in React project

This is my first time using the element framework...

How to forget the password of Jenkins in Linux

1.Jenkins installation steps: https://www.jb51.ne...

How to migrate the data directory in Docker

Table of contents View Disk Usage Disk Cleanup (D...

Detailed explanation of using MySQL where

Table of contents 1. Introduction 2. Main text 2....

Detailed explanation of several ways to export data in Mysql

There are many purposes for exporting MySQL data,...

MySQL learning tutorial clustered index

Clustering is actually relative to the InnoDB dat...

Detailed explanation of the role of static variables in MySQL

Detailed explanation of the role of static variab...

Vue event's $event parameter = event value case

template <el-table :data="dataList"&...

Some ways to solve the problem of Jenkins integrated docker plugin

Table of contents background Question 1 Error 2 E...

Let's talk about the difference between containers and images in Docker

What is a mirror? An image can be seen as a file ...