Single-table queries basis of content

You can use the where clause
1. The comparison operators:>, <,> =, <=, =,!
2. between 80 and 100: a value between 80 and 100 include: on both sides
3. in (80,90,100) is 80 or 90 or 100
4. like 'xiao%' _%, or may be,% represents any number of characters, a character represented _  
x%, at the beginning of x
% A%, comprising a
% End tt
The logical operators: operators can be used directly in a plurality of logical conditions and or not
Group by group
Tips, each of the back fields are grouped field  
After back select group can not appear in any other field in addition to the group by field
However, aggregate functions can be used in conjunction
 select group_concat(name),post,count(id) from employee group by post;
Aggregate function  
max () selecting the maximum value
min () for the minimum
AVG () averaging
sum () sums
count () Total number of seek
After having polymerizable function in conjunction with a general packet filtering criteria  
order by ordering
desc Descending
asc ascending
select * from employee order by salary desc;
Take defined limit number of
select * from employee order by salary desc limit 2; beginning to take two
select * from employee order by salary desc limit 1,2; second from the start to take two
Simple exercises 39.100.47.247
class 1 2 3  
Class size
select cls, count(name) group by cls;
Information older than 20 students of the display 2
select * from class where age>20 limit 2;
Information older than 20, sorted from high to low scores of students show 2
select * from class where age>20 order by score desc limit 2
  每个班级的平均成绩
select cls,avg(score) from class group by cls;
年龄小于18岁,且成绩大于70分的同学信息
select * from class where age < 18 and score > 70;
年龄小于20岁,且平均成绩大于70分的同学信息
select name,avg(score) from class where age < 20 group by name having avg(score) > 70;
选出成绩在70到90之间的同学信息并按年龄降序排列
select * from class where score between 70 and 90 order by age desc;
筛选出成绩不小于80分的信息。从第2条显示,显示3条。
select * from class where score>=80 limit 1,3;  
每个班级年龄大于20 的同学数量,及最高成绩。
select cls, max(score), count(name) from class where age>20 group by cls;
重点中的重点:关键字的执行优先级
1 from
2 where
条件里面不能出现聚合函数
分组之前筛选的条件
3 group by
4 聚合函数(max(id)等)
5 having 一般和聚合函数连用
分组之后筛选条件
6 select 结果
7 distinct 去重
8 order by
9 limit

Guess you like

Origin www.cnblogs.com/Darry-Ring/p/12141990.html