Problems encountered in the execution order of AND and OR in SQL statements

Problems encountered in the execution order of AND and OR in SQL statements

question

I encountered a problem when writing database SQL yesterday. The root of the problem lies in the execution priority of the AND and OR keywords in SQL statements. Let's test this issue.

Scenario

1. There is a student table Student, the table fields include Id (user primary key), Name (user name), Grade (grade), Class (class), Sex (gender). as follows:

Table Structure

2. Import ten test data into the table as follows:

Table Data

3. Now you need to find out the female students in grade 1 who are female, or the female students in class 2 who are female. The SQL statement is as follows:
select * from student where sex='女' and grade=1 or class=2
However, the result of the SQL query does not meet the requirements. The execution results are as follows:

Execution Results

The execution result also found male students in class 2, which is obviously incorrect.

4. Modify the SQL statement and add brackets. as follows:

select * from student where sex='女' and (grade=1 or class=2)

The result of the SQL query meets the requirements

analyze

From the above scenario, the key to the problem lies in the execution order of AND and OR.
According to the data, the priority of relational operators from high to low is: NOT >AND >OR
If there is an OR condition after where, OR will automatically separate the left and right query conditions.
Just like the first statement in the above scenario, its query condition is divided into two parts (or):

1. sex = 'female' and grade = 1
2. class=2

This means finding out the female students in grade 1 and the students in class 2. Does not meet the requirements of the original query.
The solution is to use brackets to distinguish the order of execution <br /> As in the second statement in the above scenario, the query condition is divided into two parts (and):

1. sex = 'female' 
2. (grade=1 or class=2)

You may also be interested in:
  • SQL statement execution order graphic description
  • Detailed explanation of SQL statement execution order
  • In-depth explanation of SQL statement execution (MySQL architecture overview -> query execution process -> SQL parsing order)
  • Learn SQL query execution order from scratch
  • This article tells you what the execution order of Sql is

<<:  Linux (center OS7) installs JDK, tomcat, mysql to build a java web project running environment

>>:  How to use axios to filter multiple repeated requests in a project

Recommend

Teach you how to implement the observer mode in Javascript

Table of contents What is the Observer Pattern? S...

Summary of several common ways to abbreviate javascript code

Table of contents Preface Arrow Functions Master ...

...

Implementation of Nginx forwarding matching rules

1. Regular expression matching ~ for case-sensiti...

Detailed explanation of MySQL injection without knowing the column name

Preface I feel like my mind is empty lately, as I...

Nginx merges request connections and speeds up website access examples

Preface As one of the best web servers in the wor...

Summary of MySQL's commonly used database and table sharding solutions

Table of contents 1. Database bottleneck 2. Sub-l...

Web page HTML code: production of scrolling text

In this section, the author describes the special...

Detailed explanation of how to restore database data through MySQL binary log

Website administrators often accidentally delete ...

MySQL uses custom functions to recursively query parent ID or child ID

background: In MySQL, if there is a limited level...

Should the Like function use MySQL or Redis?

Table of contents 1. Common mistakes made by begi...

Detailed explanation of generic cases in TypeScript

Definition of Generics // Requirement 1: Generics...

Detailed examples of Linux disk device and LVM management commands

Preface In the Linux operating system, device fil...

Native js implementation of magnifying glass component

This article example shares the specific code for...

Summary of 7 types of logs in MySQL

There are the following log files in MySQL: 1: re...