#Advanced 2: Conditional query
grammar:
select
查询列表
from
表名
where
筛选条件
Classification:
1. Filter by conditional expressions
Conditional operators: >, <,=, !=, <> ,>=, <=
2. Filter
logical operators by logical expressions : &&, || ,!, and, or, not
3. Fuzzy query
like,between and,in,is null
#--------------------------------- -------------------
#1. Filter by conditional expression
#Case 1: Query employee information with salary> 12000
SELECT * FROM employees WHERE salary>12000;
#Case 2: Query the employee name and department number whose department number is not equal to 90
SELECT last_name,employee_id FROM employees WHERE department_id<>90;
#2. Filter by logical expression
# Case 1: Query the names of employees whose salary is between 10,000 and 20,000. Salary and bonus
SELECT last_name,salary,commission_pct FROM employees WHERE salary>=10000 AND salary<=20000;
#Case 2: Query information about employees whose department number is not between 90 and 110, or whose salary is higher than 15000
SELECT * FROM employees WHERE department_id<90 OR department_id>110 OR salary>15000;
#3. Fuzzy query/
*1.like
features:
generally used with
wildcards. Wildcards:
% Any number of characters, including 0 characters
_ Any single character
*/ #Case
1: Query employee information that contains the character a in the employee name
SELECT * FROM employees WHERE last_name LIKE '%a%';
#Case 2: Query the name and salary of the employee whose third character is e and the fifth character is a
SELECT last_name,salary FROM employees WHERE last_name LIKE '__n_l%';
#Case 3: Query the name of the employee whose second character is _
SELECT last_name FROM employees WHERE last_name LIKE '_\_%';
SELECT last_name FROM employees WHERE last_name LIKE '_S_%' ESCAPE 'S';
/*2.between and
1. Use it to improve simplicity
2. Include critical values
3. Do not switch the order of the two critical values
*/ #Case
1: Query employee information with employee numbers between 100 and 120
SELECT * FROM employees WHERE employee_id BETWEEN 100 AND 120;
/*3.in
meaning: determine whether the value of a field belongs to a certain item in the in list.
Features:
1. Improve conciseness
2. List value types must be unified or compatible
*/
#Case: The job type number of the query employee is IT_PROT, AD_VP, an employee name and job type number in AD_PRES
SELECT last_name,job_id FROM employees WHERE job_id='IT_PROT' OR job_id='AD_VP';
SELECT last_name,job_id FROM employees WHERE job_id IN('IT_PROT','AD_VP','AD_PRES');
/*4.is null
= or <> cannot be used to judge the null value
and null or is not null can be used to judge the null value
*/ #Case
1: Query the names of employees without bonuses and bonus rates
SELECT last_name,commission_pct FROM employees WHERE commission_pct IS NULL;
#安全 equal<=> #Case
1: Query the names of employees without bonuses and bonus rates
SELECT last_name,commission_pct FROM employees WHERE commission_pct <=>NULL;
#Case 2: Query information about employees with a salary of 12000
SELECT last_name,salary FROM employees WHERE salary <=> 12000;
#is null pk <=>
/*
is null: Only NULL values can be judged, which is more readable. It is recommended to use
<=>: Both NULL values and ordinary values can be judged, with lower readability
*/
#测试
#1. Query the name and salary of employee number 176
SELECT
last_name,department_id,salary*12*(1+IFNULL(commission_pct,0))
AS
年薪
FROM
employees WHERE employee_id=176;
#2. Other
SELECT last_name,department_id FROM employees WHERE department_id IN(20,50);
SELECT last_name,salary FROM employees WHERE commission_pct IS NOT NULL;
SELECT last_name FROM employees WHERE last_name LIKE '__a%';
SELECT last_name FROM employees WHERE last_name LIKE '%a%e%' OR '%e%a%';
SELECT last_name,salary FROM employees WHERE commission_pct IS NULL AND salary<18000;
SELECT * FROM employees WHERE job_id NOT LIKE'IT%' OR salary=12000;
DESC departments;
SELECT DISTINCT location_id FROM departments;