supplement:
At first look-up table, be sure to follow the most basic steps, first determine what tables, then check this table to determine no restrictions, then determine whether to classify what field corresponds to the information required to determine the final again
----------------------------------------------------------------------------------------
You should produce the results of each step are treated as a new table
and then perform other operations on the table
1. Query job name and the names of all staff positions included GROUP_CONCAT
2. Query job name and the number of employees in each job contained count
the number of queries in the company 3. male employees and female employees count
4. Query name as well as the job posts the average salary avg
5. query job name and the highest salary of each position max
minimum wage job inquiries min 6. name and various posts.
7. Poll average salary of male employees and male employees, the average salary avg female employees and female employees
Aggregation function max min sum count avg can only be used after the packet
if the table is not a write group by default all of the data is a group
Statistics of the department over the age of 30 years the average wage of employees
select post,avg(salary) from emp where age > 30 group by post;
having
Now where is exactly the same is also used to filter the data
but having is following the group by the
where is the overall data to do a preliminary screening
and after having a data packet to conduct a targeted screening
Statistics of the department over the age of 30 years the average wage of employees and retention of more than 10,000 of the average wage sector
select post,avg(salary) from emp where age > 30 group by post having avg(salary) > 10000; select post,avg(salary) from emp where age > 30 group by post where avg(salary) > 10000; # 报错
Stressed: having to be used in the later group by
Execution order
Multiplicity distinct to repeat a data deduplication
Data deduplication must be exactly the same in order to re-
as long as there is not a duplication of data can not be regarded as a
select distinct id,age from emp;
Sort ascending order by default can also become Descending asc desc
SELECT * from EMP Order by salary; SELECT * from EMP Order by salary ASC; SELECT * from EMP Order by salary desc; SELECT * from EMP Order by age, salary; # the first same ascending age according to age do case then follow salary ascending to do the SELECT * from emp asc the Order by age, salary desc; # to do and then follow the salary according to age to do the same in ascending order of ascending age case
# Statistics departments in the age of the average wage of employees over the age of 10,
# and keep the average wage of more than 1000 of the department, and then sort of average wage
select post,avg(salary) from emp where age > 10 group by post having avg(salary) > 1000 order by avg(salary); select post,avg(salary) from emp where age>10 group by post having group_concat(salary)>1000 order by group_concat(salary)desc;
restrictions limit the number of pieces of data show
* the SELECT from emp limit 5; # only show the data of five the SELECT * from emp limit 5,5;
Show only limit is when only a few parameters of the representation
when the time limit has two parameters starting position of the second parameter represented by the first parameter indicates the number of impressions from the start position of the next
# Inquiry tallest man salary details # first sorted according to salary # then just take a limit to limit the SELECT * from emp the Order by salary desc limit 1;
Regular
# In programming they see the beginning reg basically with canonical correlation
select * from emp where name regexp '^j.*(n|y)$';