One relationship of complementary

Single-table queries 
a syntax sequentially
select distinct query field 1, 2 ,. query field . . from table
before the packet filter condition where
the filter conditions after having packet
group by group by
order by sort field
number of limit display;

Second, the order of execution

select distinct query field 1, 2 ,. query field . . from table name
is first performed from determines to perform is the file
and then executed before where determining a packet filter condition
and then perform group by group
and then performing a select screened
before performing order by row number sequence
and then execute limit restriction results show Article number

three, according to the priority level of SQL statements to write
a, first determine which tables db39.emp from
B, whether a filter condition like name WHERE '% I%'
. . .
z, discharge function select

four, where the filter
where the words may be used:
1. The comparison operators:> <!> = <= <> is not equal to a = #! = Not <>
SELECT ID, name from db39.emp WHERE ID> = ID. 3 and <=. 6

BETWEEN 80 and 100 2.
SELECT * WHERE ID BETWEEN from db39.emp. 3 and. 6; #> =. 3 and <=. 6

3. in (80,90,100) is 80 or 90 or 100
SELECT * from the salary in EMP WHERE ( 20000,18000,17000); SELECT * # EMP WHERE from the salary or the salary = 20000 or 18000 = 17000 = the salary;

4. like 'Egon%',% or pattern may be _,% represents any number of characters, a character represented _
select name, salary from db39.emp where name like '% i%' # claim: employee name in the employee name query comprising i letters their salary
select name, salary from db39.emp where name like '____'; # requirements: query employee name is a four-character name of its employee payroll
select name, salary from db39.emp where char_length (name) = 4; # consistent results over a

5 logical operators: can be used directly in a plurality of logic conditions Not or operator and
SELECT * WHERE from db39.emp Not BETWEEN. 3 and ID. 6;
SELECT * from EMP WHERE Not in the salary (20000,18000,17000);

Requirements: query job description empty employee name and job name
select name, post from db39.emp where post_comment is NULL; # for NULL must be is, can not be used =
the SELECT name, POST from db39.emp the WHERE IS post_comment not NULL;
#NULL refers not take any storage, the string is hollow mysql occupy storage space, i.e. not empty (NULL)

five, group by group
, if not set to only_full_group_by mode, the sub-group was complete default withdrawn * is the first personal data within the group. But an element within minutes after the finish to take a separate group group does not make sense, therefore, of the packet, the general pattern will be processed as follows
# sql_mode set to only_full_group_by, means that the future whenever a packet, packet basis can only take into
mysql> Global the sql_mode = sET "STRICT_TRANS_TABLES, only_full_group_by";

# polymerizable function group function (typically in conjunction with a packet)
SELECT POST, max (the salary) from group EMP by POST; # could not take the name element in the group, age .., can only take group name (packet based) or polymerization function

SELECT POST, min (the salary) from group EMP by POST;

SELECT POST, AVG (the salary) from group EMP by POST;

POST SELECT, SUM (the salary) from Group EMP by POST;

SELECT POST, COUNT (ID) Group from EMP by POST;

#group_concat (packet after use): extraction with the desired information; string concatenation operator
select post, group_concat (name) from EMP Group by POST;
SELECT POST, GROUP_CONCAT (name, "_ SB") from EMP Group by POST;
SELECT POST, GROUP_CONCAT (name, ":", the salary) from EMP Group by POST;
SELECT POST, GROUP_CONCAT ( the salary) from Group EMP by POST;

# supplemented concat (when not using packets): string concatenation operator
select concat ( "nAME:", name) as the name, concat ( "SAL:", salary) as EMP pay from;

# added as syntax: taken as a field or a table alias
select name as the name, salary as salary from emp; # as may be omitted
mysql> select emp.id, emp.name from emp as t1; # error
mysql> select t1.id, t1.name from emp as t1; # with MySQL> the SELECT the above mentioned id, name from emp AS T1;

# query four operations
the SELECT name, salary * 12 AS annual_salary from emp;

# group exercises
select post, group_concat (name) from emp group by post ; # query job name and the names of all staff positions included in

select post, count (id) from emp group by post; # query job name and the number of employees in each job included

select sex, count (id) from emp group by sex ; # query within the company male employees and female employees the number of

select post, avg (salary) from emp group by post; # query job name, and the average salary of each position

select sex, avg (salary ) from emp group by sex; # inquiry average salary of male employees and male employees, the average salary of female employees and female employees

select post, avg (salary) from emp where age> = 30 group by post; # statistical departments age employees aged 30 years and average wages

six, having filter (must use the group name (group by) or aggregate functions)
having the syntax and where exactly the same, but after having carried out the packet filter further
That is where can not use aggregate functions, and having that can aggregate function, which is the two of them the biggest difference

department # statistics departments older workers the average wage over the age of 30, and retains the average wage of more than 10,000 of
select post, avg (salary ) from emp the WHERE Age> = 30 Group by POST the HAVING AVG (salary)> 10000;

# stressed: having to be used later group by (do not recognize the default packet)
the SELECT * from emp the HAVING AVG (salary)> 10000; # error

seven , distinct deduplication (performed after having, and post, name, etc. belonging to the same execution level)
SELECT DISTINCT POST, AVG (the salary) from EMP WHERE Age> = 30 Group by POST HAVING AVG (the salary)> 10000;

eight, order by sorting (default ascending)
SELECT * from EMP Order by the salary ASC; # default ascending row
select * from emp order by salary desc ; # descending row
select * from emp order by age desc ; # descending row
select * from emp order by age desc , salary asc; # age descending according to the first row, and then according to pay in ascending

# Statistics of the department over the age of 10 year-old employee average wage, and keep the average wage of more than 1000 of the department, then on average wages sort
select post, avg (salary) from emp where age> 10 group by post having avg (salary) > 1000 order by avg (salary) ;

nine, limit restrictions show the number of; paging
the SELECT * from emp limit 3;
the SELECT * from emp the Order by salary desc limit 1; # display the highest paid person's information
select * from emp limit 0, 5; # tab, starting from 0, take 5 (1-5)
SELECT * from limit EMP 5, 5; # tab 5 from the beginning, taking 5 (6-10)
tab equation:
If each of the information article referred to as a count number
then the n-th information also is the start of the (n-1) * count information pieces in total, taking the future count article
is
select * from emp limit (n- 1) * count, n ;

X. regular expression
select * from emp where name regexp ' ^ jin * (n | g) $.'; # adjusting regular; regular expressions common

multi-table join query
a Cartesian product
from emp, dep, dep2, ...

Second, the connector: the two tables of correspondence between a virtual table records connected
SELECT * from the Join EMP Inner DEP = ON emp.dep_id dep.id;

# Applications:
SELECT * from EMP, DEP DEP = WHERE emp.dep_id .id and dep.name = "technology"; # is not recommended; do not even live to do with where the table
select * from emp inner join dep on emp.dep_id = dep.id where dep.name = " technology"; # logic and on a consistent

three left connection: connection basis, including on the left there is no corresponding record retention relationship
select * from emp left join dep on emp.dep_id = dep.id;

Fourth, the right connection: including a connection basis, do not correspond to retain the right record
select * from emp right join dep on emp.dep_id = dep.id;

five full connection: connection basis, including on the reservation left and right do not correspond to the record
select * from emp left join ON emp.dep_id = dep.id DEP
of Union # to re-
select * from emp right join dep on emp.dep_id = dep.id;

six multi-table joins can be a single table constantly connected to the virtual table
# Find all sectors the highest salary
T1 SELECT. * T1 from EMP AS
Inner the Join
(SELECT POST, max (the salary) from EMP Group AS MS by POST) # AS T2 commission virtual table T2
ON = t2.post t1.post
WHERE t1.salary = t2.ms
;

SELECT * from EMP AS T1 T1.
Inner the Join
(SELECT POST, max (the salary) from EMP Group AS MS by POST) AS T2
ON t1.salary = t2.ms
;
subquery (a problem solving a problem)
to a query statement parentheses, as an additional query terms to use, called a subquery

select name from emp where dep_id = ( select id from dep where name = " technology"); # subquery
select emp.name from emp inner join dep on emp.dep_id = dep.id where dep.name = " technology"; # list

# query an average age of over 25-year-old department name of
select name from dep where id in ( select dep_id from emp group by dep_id having avg (age)> 25); # subquery
select dep.name from emp inner join dep on emp.dep_id = dep.id group by dep.name having avg (age)> 25; # list

# 2 people view the lack of a department name (subquery get is someone's department the above mentioned id)
the SELECT * from emp the WHERE EXISTS (DEP the WHERE from the SELECT the above mentioned id the above mentioned id> 3); #exists usage, when () return True, the outer query will query; when the return value is False, the outer query is not a query (empty the SET)

# query each department newest recruits employees who
select t1.id, t1. name, t1.post, t1.hire_date, t2.post, t2.max_date from emp as t1 inner join (select post, max (hire_date) as max_date from emp group by post) as t2 on t1.post = t2.post where t1.hire_date = t2.max_date;




Guess you like

Origin www.cnblogs.com/1832921tongjieducn/p/11128865.html