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

Summary of pitfalls in importing ova files into vmware

Source of the problem As we all know, all network...

Example of using CSS to achieve floating effect when mouse moves over card

principle Set a shadow on the element when hoveri...

Three examples of nodejs methods to obtain form data

Preface Nodejs is a server-side language. During ...

WeChat Mini Program Basic Tutorial: Use of Echart

Preface Let’s take a look at the final effect fir...

Deep understanding of line-height and vertical-align

Several concepts Line box: A box that wraps an in...

Dealing with the problem of notes details turning gray on web pages

1. In IE, if relative positioning is used, that is...

Detailed explanation of galera-cluster deployment in cluster mode of MySQL

Table of contents 1: Introduction to galera-clust...

Detailed explanation of transaction isolation levels in MySql study notes

background When we talk about transactions, every...

A brief discussion on the correct approach to MySQL table space recovery

Table of contents Preliminary Notes Problem Repro...

A brief summary of my experience in writing HTML pages

It has been three or four months since I joined Wo...

Detailed explanation of the API in Vue.js that is easy to overlook

Table of contents nextTick v-model syntax sugar ....

Analysis of the process of deploying pure HTML files in Tomcat and WebLogic

1. First, the pure HTML file must have an entry i...

Dockerfile implementation code when starting two processes in a docker container

I want to make a docker for cron scheduled tasks ...

Detailed explanation of web page loading progress bar (recommended)

(When a web page is loading, sometimes there is t...

Practical method of deleting files from Linux command line

rm Command The rm command is a command that most ...