The database table queries, single-table, multi-table, subqueries

The basic query and methods
from
the WHERE
Group by
the HAVING
DISTINCT
the Order by
limit
....

连表
inner join
left join
right join
union

Subqueries

 

 

Writing order
select id, name from emp where id > 3 and id <6;
execution order
from # which is determined in the end station table
where # The condition over screening data
out of some fields filter out data select #

select * from emp \ G; particularly when typesetting table fields when the results might appear confusing phenomenon you can query plus \ G to standardize the query result


# 1 query id 3 less than or equal to 6 data
select * EMP WHERE ID from> = ID. 3 and <=. 6;
SELECT * from EMP WHERE BETWEEN. 3 and ID. 6;
statement above is exactly equivalent to
# 2 data query pay 20,000 or 18,000 or 17,000
select id, name from emp where salary 20000 or salary of 18,000 = = or salary = 17000;
the SELECT the above mentioned id, name from emp in the WHERE salary (20000,18000,17000);
# 3. query employee names and salaries of employees o name contains the letters of
fuzzy matching like
%: more than match arbitrary character
_: matches any character
select name, salary from emp where name like '% O%';
# 4. employee name query is a four-character name of the employee's salary to its
select name, salary from emp where name like '____';
# 5. query id is less than 3 or greater than 6, the data
select * from emp where id <3 or id> 6;
* from EMP WHERE ID SELECT BETWEEN. 3 and Not. 6;
# 6. The query is not salary range 20000,18000,17000 data
SELECT ID, name from the salary Not in EMP WHERE (20000,18000,17000);
# 7. The query job description empty of staff for the name and job name can only be used to determine when the null is not used =
the SELECT name, POST from the WHERE emp post_comment = null;
the SELECT name, the WHERE post_comment POST from emp is null;

MySQL is not case sensitive


group by packet

# 1 by department groups
select * from emp group by post;
after grouping should do the minimum unit is set, then should not show a single data information within the group

after group in MySQL can only get field information packets can not be directly obtain additional information fields
, but you can get by the introduction of other methods (aggregate functions)

If your MySQL not being given instructions strict mode is not set
Show the Variables like '% the MODE%';
the SET the session of the current window effectively
set global globally valid
set global sql_mode = "STRICT_TRANS_TABLES, only_full_group_by";
the SELECT * from emp Group by POST;
the SELECT the above mentioned id, EMP by POST from Group name;
select name from Group EMP by POST;
# 2. Get the maximum salary for each department aggregation function SUM COUNT AVG min max
select POST, max (the salary) from Group EMP by POST;
to field aliasing
select post as 'departments', max (salary) as 'maximum wage' from emp group by post;
select post 'department', max (salary) 'maximum wage' from emp group by post;
the minimum wage # each department
select post , min (salary) from emp Group by POST;
# average salary for each department
select post, avg (salary) from emp group by post;
The sum of the salaries for each department #
POST the SELECT, SUM (salary) from emp Group by POST;
# number of people in each department of
the SELECT POST, COUNT (Age) from emp Group by POST;
the SELECT POST, COUNT (salary) from emp Group by POST;
the SELECT POST, COUNT ( id) from Group EMP by POST;
SELECT POST, cOUNT (the post_comment) from Group EMP by POST;
when the number of packets in the statistical fill any non null field count can be done, can be recommended to use the unique identification data fields
such as id field
"" "
aggregate function will automatically single data within each group do the calculation you want, you do not need to consider

" ""

All the names of the students under the name of the department after the query packet # 3 and each department
select post, group_concat ( name) from Group EMP by pOST;
select post, GROUP_CONCAT ( 'the DSB', name) from Group EMP by pOST;
GROUP_CONCAT () each data packet can get the specified field (may be a plurality) corresponding to the value of the


select post, GROUP_CONCAT (name, ":", the salary) from Group EMP by POST;


the concat
select concat ( "NAME:", name), concat ( "SAL:", salary) from emp;


tips:
concat is used to help you splice data
under concat not use groupings
used after group_concat packet


# query each employee the annual salary of
the SELECT name, salary from emp * 12;


# beginning lookup table, be sure to follow the most basic steps, first determine what tables, then check this table to determine no restrictions, and then determine the need for classification, and finally determine what field corresponds to the information you need
. "" "+

result you should be generated each step are treated as a new table
and then perform other operations on the table
." ""
1. query job name and job contains all employee name 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 job name, and the average salary of each position AVG
5. names and positions inquiry max maximum salary of each position
6. queries job name and position of each minimum wage min
7. Discover the average salary of male employees and male employees, the average salary of female employees and female employees AVG

"" "
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
"" "
writing order
select
from
where
group by

Execution order
from
WHERE
Group by
SELECT

8, statistical departments in the age of the average wage of employees over 30 years old

# To get young workers over the age of 30
the SELECT POST, AVG (salary) from emp the WHERE Age> 30 Group by POST;
"" "
write sql statement must not breath when finished
early to write according to the procedure step by step
written step then look at the results of the query after the current result and then further on to write
"" "

having
now is where exactly 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
1, the statistical departments older workers the average wage over the age of 30,
and retains the average wage of more than 10,000 of the department
the SELECT POST, AVG (salary) from emp the WHERE Age> 30 Group by POST the HAVING AVG (salary)> 10000;
the SELECT POST, AVG (salary) from emp where age> 30 group by post where avg (salary)> 10000; # error

# emphasized: having the rear group must be used by
SELECT * from EMP HAVING AVG (the salary)> 10000; error #

The execution order
from
the WHERE
Group by
the HAVING
the SELECT


DISTINCT to heavy
multi-duplicated data is a deduplication
"" "
deduplication data must be exactly the same in order to re-
as long as there is duplication of data a different can not be regarded as the
" ""
the SELECT DISTINCT the above mentioned id , age from emp;



execution order
from
WHERE
Group by
HAVING
SELECT
DISTINCT


order sort by
default ascending asc
can become descending desc

SELECT * from EMP order by the salary;
SELECT * from EMP order by the salary asc;
SELECT * from EMP order by salary desc;
SELECT * from EMP Order by age, salary; # first according to age in ascending age and then follow the salary made the same scenario ascending
select * from emp order by age asc , salary desc; # first according to age in ascending age same then in accordance with the case where the salary in ascending


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

limit restrictions Article data show the number of
select * from emp limit 5; # only show data of five
the SELECT * from emp limit 5,5;

"" "
When the time limit is only one parameter represents the only show a few
when there are two limit when the first argument starting position of the second parameter indicates the number of parameter indicates the starting position from the beginning of the next show

. "" "
the highest human inquiry salary details #
# to pay in accordance with the sort
# then limit Restrict take a

SELECT * from EMP Order by desc limit the salary. 1;


# just beginning to see reg basically associated with the positive programming in


the regular
select * from emp where name regexp ' ^ j * (n | y). $ ';


Jason
jssdsdsay
JY
JNN




multi-table queries

"" "
table query into two categories
1. linked table query
2. subquery
" ""
the SELECT * from emp, DEP;The result is a Cartesian product


# queries sector employees and departments of information technology department



have a special table to help you do even approach
the connector (inner join)

Left connection (left join)

the right connection (right join)

fully connected (union) # as long as the left and right connections sql statement to add a union becomes fully connected

select * from emp left join dep on emp.dep_id = dep. ID
Union
SELECT * from the Join EMP right DEP = ON emp.dep_id dep.id;


subquery
query results as a table a further query sql statement





select name from dep where id = ( select dep_id from emp where name = 'Jason');

# 2. each department employee newest recruits
# ideas: first check the latest recruits each department, press department correspondence with the table query

select t1.id, t1.name, t1.hire_date, t1 . .post, EMP AS T1 from T2 *
Inner the Join
(SELECT POST, max (the hire_date) from EMP Group AS MAX_DATE by POST) AS T2
ON = t2.post t1.post
WHERE t1.hire_date = t2.max_date
;


# give table aliases
# can to check out the virtual table aliases
# can play an alias to the field

Guess you like

Origin www.cnblogs.com/zhangchaocoming/p/11391987.html