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

Button is stretched on both sides in IE

When you write buttons (input, button), you will f...

How to use CocosCreator to create a shooting game

Analyze the production steps: 1. Prepare resource...

HTML form_PowerNode Java Academy

1. Form 1. The role of the form HTML forms are us...

What to do if you forget your mysql password

Solution to forgetting MySQL password: [root@loca...

Teach you how to quickly install Nginx in CentOS7

Table of contents 1. Overview 2. Download the Ngi...

How to build a DHCP server in Linux

Table of contents 1. Basic knowledge: 2. DHCP ser...

How to use JS code compiler Monaco

Preface My needs are syntax highlighting, functio...

Examples of using Docker and Docker-Compose

Docker is an open source container engine that he...

MySQL 8.0.12 installation configuration method and password change

This article records the installation and configu...

Summary of front-end knowledge in the Gokudō game

background In the early stages of learning Japane...

Writing a web calculator using javascript

This article mainly records the effect of using j...

MySQL transaction analysis

Transaction A transaction is a basic unit of busi...

MySQL Router implements MySQL read-write separation

Table of contents 1. Introduction 2. Configure My...

Design Reference Beautiful and Original Blog Design

All blogs listed below are original and uniquely ...