Tips for Mixing OR and AND in SQL Statements

Tips for Mixing OR and AND in SQL Statements

Today, there is such a requirement. If the logged-in person is a customer service representative, the order will be queried for "this customer service representative" and the orders that have not been matched with a customer service representative. At first, I thought of spelling "or assigned_id is null" directly in the SQL statement. After testing, I found that in this case, the other previous conditions are useless.

In this case, the first condition i.server_org_id = 4 is no longer applicable. From this we can see that AND has a higher priority than OR. The previous AND statement is executed first, and then the following OR statement is executed. Therefore, the data retrieved is not the data I want.

Later I thought about it again and found that I could first find out the corresponding assigned_id, as follows

The only data found in this way is the assigned_id 153

Final version:

Finally, all the correct data is read out. From this, we can see that () has a higher priority than AND. () is executed first, and then AND is executed.

The above is the full content of this article. I hope it will be helpful for everyone’s study. I also hope that everyone will support 123WORDPRESS.COM.

You may also be interested in:
  • Mysql multi-condition query statement with And keyword
  • Query process and optimization method of (JOIN/ORDER BY) statement in MySQL
  • MySQL briefly understands how "order by" works
  • .NET Core Dapper implementation method of operating MySQL database

<<:  A detailed introduction to the three installation methods of rpm, yum and source code under Linux

>>:  Summary of practical skills commonly used in Vue projects

Recommend

...

Solution to 1045 error in mysql database

How to solve the problem of 1045 when the local d...

Detailed Analysis of the Differences between break and last in Nginx

Let's talk about the difference first last, t...

MySQL 5.7.27 installation and configuration method graphic tutorial

MySQL 5.7.27 detailed download, installation and ...

In-depth explanation of Vue multi-select list component

A Multi-Select is a UI element that lists all opt...

Why does MySQL database index choose to use B+ tree?

Before further analyzing why MySQL database index...

Detailed explanation of docker entrypoint file

When writing a Dockerfile, include an entrypoint ...

HTML basic summary recommendation (text format)

HTML text formatting tags 標簽 描述 <b> 定義粗體文本 ...

XHTML Getting Started Tutorial: XHTML Hyperlinks

It is no exaggeration to say that hyperlinks conne...

Tutorial on installing MySQL database and using Navicat for MySQL

MySQL is a relational database management system ...

Web page header optimization suggestions

Logo optimization: 1.The logo image should be as ...

Comparison of various ways to measure the performance of JavaScript functions

Table of contents Overview Performance.now Consol...

HTML table tag tutorial (32): cell horizontal alignment attribute ALIGN

In the horizontal direction, you can set the cell...

Detailed explanation of how to use grep to obtain MySQL error log information

To facilitate the maintenance of MySQL, a script ...