In the addition, deletion, modification and query of mysql, the query is undoubtedly the most important. The addition, deletion, and modification are relatively simple, but the query is much more complicated. This article tries to comprehensively analyze the query statement.
First import the database to navicat or other mysql clients. The database in this article is not a simple table, but a complete employee system, which makes learning more realistic.
Database file:
https://pan.baidu.com/s/19pDAvmrPy2KYsfHPtbw0lQ?pwd=yzgr
There are the following tables. When it is used, it will be specified.
It is recommended to look at each table to get familiar with the fields. These tables are very useful for practice.
SELECT * FROM table name
This is the simplest query, which is generally not used in a formal environment, because the table data in a formal environment may be very large, and directly querying the entire table may cause the system to freeze.
We query a table regions with less data, which is a region table.
SELECT * FROM regions;
Query by column name
A table may have many columns, sometimes we only want to query a few columns, we can use a statement similar to the following.
SELECT 列名1,列名2,...列名n FROM 表名;
For example, we now want to query the employee id, name and salary of the employees table.
SELECT employee_id,last_name,salary FROM `employees`
Aliases for column names
The main function of the alias is to facilitate reading. You can use the as keyword to alias the employee_id as empl_id, which is shorter. As is not an English word. It is an abbreviation for alias. as can be omitted. For example, last_name lname does not need to write as, ** Another point is that aliases can be wrapped in double quotes. The main purpose of this is that some aliases may have spaces. **For example, salary "salary", I think there is a space between the word salary. Using single quotation marks will not report an error, but this is because the syntax of mysql is not rigorous. According to the ANSI SQL standard, double quotation marks should be used, and Oracle directly reports an error with single quotation marks.
SELECT employee_id as empl_id,last_name lname,salary "工 资" FROM `employees`
Use DISTINCT to deduplicate
For example, we need to know how many department_ids there are in the employees table. The results found by the following statement are very repetitive. Use the distinct keyword to remove the duplicates.
Error conditions:
It is not acceptable to write the following, because the number of rows obtained by deduplication of salary and department_id does not match, and an error will be reported directly.
There is no mistake in writing this way, which means deduplication of the query results of department id and salary.
Null and Null Values Participate in Operations
The employee table has a commission_pct field, which represents performance. It can be seen that some employees have no performance, the performance is null, and null is not equal to 0, nor is it equal to ''. Null means no value. If you use a null value to participate in the operation, then the result is still null.
For example, the annual salary is equal to the monthly salary salary*(1+commission_pct)*12. Because some employees’ commission_pct is null, the calculated annual salary is also null. For mysql, this is reasonable, but for business it is Unreasonable, if the commission_pct as 0 is not enough? This idea will lead you into a misunderstanding, null is not equal to 0.
It is also possible to realize the function of annual salary. Null is actually equivalent to no value assignment. If it is null, we can assign the initial value to 0. This function can be realized through the IFNULL statement (described later). In this way, the function of annual salary can be realized.
backtick handling keywords
If you execute the following statement, you will find an error. This is because order is a keyword. There happens to be a table called order here. To make SQL run correctly, you can use backticks to process keywords.
SELECT * from order
It can be written as follows.
SELECT * from `order`
constant query
Sometimes we want to add something in front of the query result, which can be achieved through constant query, which can be a string or a value, and the string must have double quotes.
Show table structure with DESC
Use desc or describe.
filter query
The easiest is to use the where statement.
There are still details to say here. Under Windows, strings are also case-insensitive, but under Linux, they are case-sensitive. Changing 'King' to 'king' here can also check the result.
practise
[Title]
1. Query the total salary of employees for 12 months, and alias it as ANNUAL SALARY
2. Query the data after removing the duplicate job_id in the employees table
3. Query the name and salary of employees whose salary is greater than 12000
4. Query the employee number as The name and department number of 176 employees
5. Display the structure of the departments table and query all the data in it
[Answer]
1. Query the employee's 12-month salary sum, and alias it as ANNUAL SALARY
SELECT employee_id , last_name,salary * 12 "ANNUAL SALARY" FROM employees;
SELECT employee_id,last_name,salary * 12 * (1 + IFNULL(commission_pct,0)) "ANNUAL SALARY" FROM employees;
2. Query the data after removing the duplicate job_id in the employees table
SELECT DISTINCT job_id FROM employees;
3. Query the name and salary of employees whose salary is greater than 12000
SELECT last_name,salary FROM employees WHERE salary>12000;
4. Query the name and department number of the employee whose employee number is 176
SELECT last_name,department_id FROM employees WHERE employee_id=176;
5. Display the structure of the table departments and query all the data in it
DESC departments;
SELECT * FROM departments;