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

Perfect solution to Google Chrome autofill problem

In Google Chrome, after successful login, Google ...

Detailed explanation of fuser command usage in Linux

describe: fuser can show which program is current...

MYSQL subquery and nested query optimization example analysis

Check the top 100 highest scores in game history ...

How to set static IP in centOS7 NET mode

Preface NAT forwarding: Simply put, NAT is the us...

Vue.js implements the code of clicking the icon to zoom in and leaving

The previous article introduced how Vue can reali...

Detailed steps to deploy SpringBoot projects using Docker in Idea

Preface Project requirements: Install the Docker ...

JavaScript canvas realizes dynamic point and line effect

This article shares the specific code for JavaScr...

The most commonly used HTML escape sequence

In HTML, <, >, &, etc. have special mean...

Graphical steps of zabbix monitoring vmware exsi host

1. Enter the virtualization vcenter, log in with ...

Details on using order by in MySQL

Table of contents 1. Introduction 2. Main text 2....

Detailed explanation of location and rewrite usage in nginx

1. Summary of location usage Location can locate ...

Docker installs Elasticsearch7.6 cluster and sets password

Starting from Elasticsearch 6.8, free users are a...