Solve the problem of combining AND and OR in MySQL

Solve the problem of combining AND and OR in MySQL

As shown below:

SELECT prod_name,prod_price FROM products WHERE vend_id = 1002 OR vend_id = 1003 AND prod_price >= 10;

The result returned by the above statement is not what we want.

analyze:

The reason lies in the order of calculation. SQL prioritizes AND operators before OR operators. When SQL sees the above WHERE clause, the operators are incorrectly combined because AND has a higher precedence in the calculation order.

The solution to this problem is to use parentheses to explicitly group corresponding operators.

Please see the following SELECT statement

 SELECT prod_name,prod_price
    FROM products

    WHERE ( vend_id = 1002 OR vend_id = 1003 ) AND prod_price >= 10;

Supplementary knowledge: Mysql | Combined where clause to filter data (AND, OR, IN, NOT)

MySQL allows the use of multiple where clauses, and the combination of where clauses allows the use of two methods: AND and OR clauses.

Operation symbols in the database: AND, OR, IN, NOT.

AND :

SELECT * FROM products WHERE products.vend_id = 1003 AND products.prod_price <= 10;

OR :

SELECT * FROM products WHERE products.vend_id = 1002 OR products.vend_id = 1003;

IN :

It is recommended not to use OR in clauses where IN can be used. IN has good performance and is easy to understand.

SELECT * FROM products WHERE products.vend_id IN (1002,1003);

NOT:

MySQL supports NOT only in the negation of IN, BETWEEN, and EXISTS clauses, which is different from most other databases that support various conditions.

SELECT * FROM products WHERE products.vend_id NOT IN (1002,1003);

Notice:

In a clause that contains both AND and OR, MySQL prioritizes AND operations. It is generally recommended to use () to determine the processing order and eliminate ambiguity.

for example:

SELECT * FROM products WHERE (products.vend_id= 1002 OR products.vend_id=1003) AND prod_price >= 10;

The above article on solving the problems caused by the combination of AND and OR in MySQL is all the content that the editor shares with you. I hope it can give you a reference. I also hope that you will support 123WORDPRESS.COM.

You may also be interested in:
  • MySQL conditional query and or usage and priority example analysis
  • Priority analysis of and or queries in MySQL

<<:  How to use Vue cache function

>>:  A more elegant error handling method in JavaScript async await

Recommend

Detailed tutorial on installing Python 3 virtual environment in Ubuntu 20.04

The following are all performed on my virtual mac...

MySQL 8.0.16 winx64 installation and configuration method graphic tutorial

I just started learning about databases recently....

Example of how to set up a Linux system to automatically run a script at startup

Preface Hello everyone, I am Liang Xu. At work, w...

Pure CSS code to achieve drag effect

Table of contents 1. Drag effect example 2. CSS I...

Summary of Linux user groups and permissions

User Groups In Linux, every user must belong to a...

SQL implementation of LeetCode (196. Delete duplicate mailboxes)

[LeetCode] 196.Delete Duplicate Emails Write a SQ...

Why should you be careful with Nginx's add_header directive?

Preface As we all know, the nginx configuration f...

A detailed summary of HTML tag nesting rules suitable for beginners

I have been relearning HTML recently, which can be...

Implementation of Redis master-slave cluster based on Docker

Table of contents 1. Pull the Redis image 2. Crea...

Reasons why MySQL cancelled Query Cache

MySQL previously had a query cache, Query Cache. ...

How to install Solr 8.6.2 in Docker and configure the Chinese word segmenter

1. Environment version Docker version 19.03.12 ce...

How to quickly install RabbitMQ in Docker

1. Get the image #Specify the version that includ...

Sharing experience on the priority of CSS style loading

During the project development yesterday, I encoun...

Web Design: Web Music Implementation Techniques

<br />When inserting music into a web page, ...