(10) Logical condition priority rule sorting in MySQL (unfinished version)

1. MySQL logical condition

 Logical conditions combine the results of two comparison conditions to produce a single result based on those conditions, or reverse the results of a single condition. Returns the row when all conditions evaluate to true.

Multiple conditions can be used in the WHERE clause with AND and OR operators.

Example 1: Query the name and salary of employees whose salary is 8000 in the employees table and whose name contains e.

select last_name,salary from employees
where salary = 8000 and last_name like '%e%';

Example 2: Query the name and salary of employees whose salary is 8000 or whose name contains e in the employees table. 

select last_name,salary from employees
where salary = 8000 or last_name like '%e%';

Example 3: Query the names of employees whose names do not contain u in the employees table.

select last_Name from employees
where last_name not like '%u%';

2. Priority rules in MySQL 

In the example in the picture, there are two conditions:
the first condition is that the job_id is AD_PRES and the salary is higher than 15,000.

The second condition is that the job_id is SA_REP.

The example in the picture has two conditions:
the first condition is that the job_id is AD_PRES or SA_REP.

The second condition is that the salary is higher than $15,000 

3. Use ORDER BY to sort 

 Sorts the rows returned in an ambiguous query result. The ORDER BY clause is used for sorting.

If an ORDER BY clause is used, it must be at the end of the SQL statement.

 The execution sequence of the SELECT statement is as follows:

  • FROM clause
  • WHERE child clause
  • SELECT clause
  • ORDER BY child clause

1. Sort by column name 

Example 1: Query all employees in the employees table, display their ID, name and salary, and sort them in ascending order of salary.
 

select employee_id,last_Name,salary from employees
order by salary; 

 

select employee_id,last_Name,salary from employees
order by salary asc;  

 Example 2: Query all employees in the employees table, display their ID, name and salary, and sort them in descending order of salary.

select employee_id,last_Name,salary from employees
order by salary desc;  

 

Guess you like

Origin blog.csdn.net/m0_62735081/article/details/127299101