As shown below:
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 :
OR :
IN : It is recommended not to use OR in clauses where IN can be used. IN has good performance and is easy to understand.
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.
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:
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:
|
<<: How to use Vue cache function
>>: A more elegant error handling method in JavaScript async await
The following are all performed on my virtual mac...
I just started learning about databases recently....
Preface Hello everyone, I am Liang Xu. At work, w...
Table of contents 1. Drag effect example 2. CSS I...
User Groups In Linux, every user must belong to a...
[LeetCode] 196.Delete Duplicate Emails Write a SQ...
Preface As we all know, the nginx configuration f...
I have been relearning HTML recently, which can be...
Table of contents 1. Pull the Redis image 2. Crea...
MySQL previously had a query cache, Query Cache. ...
1. Environment version Docker version 19.03.12 ce...
1. Get the image #Specify the version that includ...
Limit usage When we use query statements, we ofte...
During the project development yesterday, I encoun...
<br />When inserting music into a web page, ...