Table of contents
1. Query a single field in the table.
2. Query multiple fields in the table.
1. Filter by conditional expression
2. Filter by logical expression
Supplement: Security equals <=>
Advanced 1: Basic query
Syntax: select query list from table name;
Features:
1. The query list can be: fields, constant values, expressions, and functions in the table
2. The query result is a virtual table
1. Query a single field in the table.
like
SELECT
last_name
FROM
employees;
search result:
2. Query multiple fields in the table.
like:
SELECT
last_name,
salary,
email
FROM
employees;
search result:
3. Query all fields in the table.
like:
SELECT
employee_id,
first_name,
last_name,
salay,
.........
或
SELECT
*
FROM
employees;
search result:
4. Query the constant value.
like:
SELECT 100;
search result:
5. Query expression.
like:
SELECT 100*98;
search result:
6. Query function.
like:
SELECT version();
search result:
7. Give an alias.
Features: ① Easy to understand; ② If the fields to be queried have duplicate names, they can be distinguished by using aliases.
Method 1. Use AS
like:
SELECT 100*98 AS 结果;
operation result:
SELECT
last_name AS 姓,
fist_name AS 名
FROM
employees;
search result:
Method 2: Use spaces
like:
SELECT
last_name 姓,
first_name 名
FROM
employees;
search result:
Case: Query salary and display the result as out put
SELECT
salary AS "out put"
FROM
employees;
search result:
8. Remove weight.
Case: Query all department numbers involved in the employee table.
SELECT
depatment_id
FROM
employees;
search result:
SELECT DISTINCT department_id FROM employees;
search result:
9. The function of + sign
The functions of the + sign in Java: ① Operator, both operands are numerical; ② Connector, as long as one operand is a string.
The + sign in mysql has only one function: operator. ① Both operands are of numeric type, and the addition operation is performed; ② One of the operands is of character type, and an attempt is made to convert the character type value into a numeric type. If the conversion is successful, the addition operation is continued; if the conversion fails, the character type value is converted to a numeric type. The value is converted to 0; ③ As long as one of them is null, the result will definitely be null.
Case: Query the employee's first name and last name to be connected into one field and displayed as name
SELECT last_name+first_name AS 姓名 FROM employees;
search result:
SELECT CONCAT('a','b','c') AS 结果;
operation result:
SELECT CONCAT(last_name,first_name) AS 姓名 FROM employees;
search result:
Advanced 2: Conditional query
/*
grammar:
SELECT
query list
FROM
Table Name
where
filter conditions;
Category: 1. Filter by conditional expression Conditional operator: > < = <>(!=) <= >=
2. Filter by logical expression Logical operator: && (and) || (or)! (not)
3. Fuzzy query like between and in is null
*/
1. Filter by conditional expression
Case 1: Query employee information with salary > 12,000
SELECT * FROM employees WHERE salary>1200;
search result:
Case 2: Query the employee name and department number whose department number is not equal to 90
SELECT last_name,department_id FROM employee WHERE department_id<>90;
search result:
2. Filter by logical expression
Case 1: Query the name, salary and bonus of employees whose salary is between 10,000 and 20,000
SELECT last_name,department_id FROM employee WHERE department_id<>90;
search result:
Case 2: Query employee information whose department number is not between 90 and 110, or whose salary is higher than 15,000
SELECT last_name,department_id FROM employee WHERE department_id<>90;
search result:
3. Fuzzy query
1、like
Features: ① Generally used with wildcards
Wildcard: % (any multiple characters, including 0 characters); _ (any single character)
Case 1: Query the information of employees whose names contain the character a
SELECT
*
FROM
employees
WHERE
last_name LIKE '%a%';
search result:
Case 2: Query the name and salary of employees whose third character is e and whose fifth character is a
SELECT
last_name,
salary
FROM
employees
WHERE
last_name LIKE '__e_a%';
search result:
Case 3: Query the employee names whose second character is _
SELECT
last_name
FROM
employees
WHERE
last_name LIKE '_\_%';或last_name LIKE '_A_%' ESCAPE 'A';
search result:
2、between and
①Using between and can improve the simplicity of the statement
②Include critical value
③Do not change 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;
search result:
3、in
Meaning: Determine whether the value of a field belongs to an item in the in list
Features: ① Use in to improve the simplicity of statements
②The value types of the in list must be consistent or compatible
Case: Querying the employee's job number is an employee name and job number among IT_PROG, AD_VP, and AD_PRES
SELECT
last_name,
job_id
FROM
employees
WHERE
job_id IN ('IT_PROG','AD_VP','AD_PRES');
search result:
4、is null
= or <> cannot be used to determine null values
is null or is not null can determine the null value
Case: Query the names and bonus rates of employees without bonuses
SELECT
last_name,
commission_pct
FROM
employees
WHERE
commission_pct IS NULL;
search result:
Supplement: Security equals <=>
Case 1: Query the names and bonus rates of employees without bonuses
SELECT
last_name,
commission_pct
FROM
employees
WHERE
commission_pct <=> NULL;
search result:
Case 2: Query employee information with salary of 12,000
SELECT
*
FROM
employees
WHERE
salary <=> 12000;
search result:
The difference between is null and <=>
IS NULL: Only NULL values can be judged, which is more readable.
<=>: It can judge both NULL values and ordinary values, and its readability is low.