10.4.1where语句
#!/usr/bin/env python
-- coding:utf-8 --
比较运算 > < = >= <= != <>
范围筛选
# 多选一 字段名 in (值1,值2,值3)
# 20000,30000,3000,19000,18000,17000
# select * from employee where salary in (20000,30000,3000,19000,18000,17000)
# 在一个模糊的范围里 between 10000 and 20000
# 在一个数值区间 1w-2w之间的所有人的名字
# select emp_name from employee where salary between 10000 and 20000;
# 字符串的模糊查询 like
# 通配符 % 匹配任意长度的任意内容
# 通配符 _ 匹配一个字符长度的任意内容
# 正则匹配 regexp 更加细粒度的匹配的时候
# select * from 表 where 字段 regexp 正则表达式
# select * from employee where emp_name regexp '^j[a-z]{5}'
逻辑运算 - 条件的拼接
# 与 and
# 或 or
# 非 not
# select * from employee where salary not in (20000,30000,3000,19000,18000,17000)
身份运算 - 关于null is null /is not null
# 查看岗位描述不为NULL的员工信息
# select * from employee where post_comment is not null;
查看岗位是teacher且名字是jin开头的员工姓名、年薪
#select emp_name,salary*12 from employee where post='teacher' and emp_name like 'jin%'
#select emp_name,salary*12 from employee where post='teacher' and emp_name regexp '^jin.*'
10.4.2分组group by
#!/usr/bin/env python
-- coding:utf-8 --
分组 group by
select * from employee group by post
会把在group by后面的这个字段,也就是post字段中的每一个不同的项都保留下来
并且把值是这一项的的所有行归为一组
聚合 把很多行的同一个字段进行一些统计,最终的到一个结果
# count(字段) 统计这个字段有多少项
# sum(字段) 统计这个字段对应的数值的和
# avg(字段) 统计这个字段对应的数值的平均值
# min(字段)
# max(字段)
分组聚合
# 求各个部门的人数
# select count(*) from employee group by post
# 求公司里 男生 和女生的人数
# select count(id) from employee group by sex
# 求各部门的平均薪资
# 求各部门的平均年龄
# 求各部门年龄最小的
# select post,min(age) from employee group by post
# 求各部门年龄最大的
# 求各部门薪资最高的
# 求各部门薪资最低的
# 求最晚入职的
# 求最早入职的
# 求各部门最晚入职的
# 求各部门最早入职的
求部门的最高薪资或者求公司的最高薪资都可以通过聚合函数取到
但是要得到对应的人,就必须通过多表查询
总是根据会重复的项来进行分组
分组总是和聚合函数一起用 最大 最小 平均 求和 有多少项
10.4.3having_orderby_limit
#!/usr/bin/env python
-- coding:utf-8 --
having 条件 # 过滤 组
部门人数大于3的部门
select post from employee group by post having count(*) > 3