question I encountered a problem when writing database SQL yesterday. The root of the problem lies in the execution priority of the AND and OR keywords in SQL statements. Let's test this issue. Scenario 1. There is a student table Student, the table fields include Id (user primary key), Name (user name), Grade (grade), Class (class), Sex (gender). as follows: Table Structure 2. Import ten test data into the table as follows: Table Data 3. Now you need to find out the female students in grade 1 who are female, or the female students in class 2 who are female. The SQL statement is as follows: Execution Results The execution result also found male students in class 2, which is obviously incorrect. 4. Modify the SQL statement and add brackets. as follows: select * from student where sex='女' and (grade=1 or class=2) The result of the SQL query meets the requirements analyze From the above scenario, the key to the problem lies in the execution order of AND and OR. 1. sex = 'female' and grade = 1 2. class=2 This means finding out the female students in grade 1 and the students in class 2. Does not meet the requirements of the original query. 1. sex = 'female' 2. (grade=1 or class=2) You may also be interested in:
|
<<: Linux (center OS7) installs JDK, tomcat, mysql to build a java web project running environment
>>: How to use axios to filter multiple repeated requests in a project
Install mysql5.7.18 on CentOS6.7 1. Unzip to the ...
background nginx-kafka-module is a plug-in for ng...
In a cluster with master-slave replication mode, ...
Before officially using Docker, let's first f...
Table of contents background LIMIT Optimization O...
How to change the password in MySQL 5.7.18: 1. Fi...
Table of contents Prepare Five weapons for…in Obj...
Alibaba Cloud Server cannot connect to FTP FileZi...
The difference between replace into and insert in...
This article example shares the specific code of ...
Table of contents 1. Basic Example 2. Set the sco...
This article mainly introduces the sample code of...
Linux installation JDK1.8 steps 1. Check whether ...
Pseudo-arrays and arrays In JavaScript, except fo...
Some tips for deep optimization to improve websit...