设为首页 加入收藏

TOP

python — 表的操作(二)(一)
2019-09-14 00:55:45 】 浏览:75
Tags:python 操作

1.单表查询

单表查询语法:

select distinct 字段1,字段2... from 表名
                              where 条件
                              group by field
                              having 筛选
                              order by field
                              limit 限制条数

关键字执行的优先级:

from
where
group by
select
distinct
having
order by
limit

1.1 简单查询

1.select语句:

  • select * from 表;
  • select 字段,字段.. from 表;
  • select distinct 字段,字段.. from 表; # 按照查出来的字段去重
  • select 字段*5 from 表; # 给查出来的字段做四则运算 (字段时int类型)
  • select 字段 as 新名字,字段 as 新名字 from 表; # 给查出来的字段重命名
  • select 字段 新名字 from 表;给查出来的字段重命名

2.定义显示的格式 concat

  • 1.concat() 函数 用于字符串拼接

    例:select concat('姓名: ',emp_name,' 年薪: ', salary*12) as annual_salary
    from employee;

  • 2.concat_ws() 第一个参数为分隔符

    例:select concat_ws(':',emp_name,salary*12) as annual_salary
    from employee;

1.2 where语句

where语句不能与聚合函数合用

1.比较运算

> 、< 、= 、>= 、<= 、!= 或<> (不等于)

用于数值比较(不会用于做字符串的比较)

2.范围筛选

  • 1.多选一 :字段名 in (值1,值2,值3)

    in (值1,值2,值3) 相当于 or

    例:select * from employee where salary in (20000,30000,3000,19000,18000,17000)

  • 2.在一个模糊的范围里

    • 1.在一个数值区间 between 值1 and 值2

      # 查薪资在1w-2w之间的所有人的名字
      select emp_name from employee where salary between 10000 and 20000;
    • 2.字符串的模糊查询 like

      通配符 % 匹配任意长度的任意内容

      select * from employee where emp_name like '程%';# 查询以程开头的
      select * from employee where emp_name like '%n';# 查询以n结尾的
      select * from employee where emp_name like '%n%';# 查询包含n的

      通配符 _ 匹配一个字符长度的任意内容

    • 3.正则匹配 regexp 用于更加细粒度的匹配的时候

      select * from 表 where 字段 regexp 正则表达式

      select * from employee where emp_name regexp '^j[a-z]{5}'

      查看岗位是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.*'

3.逻辑运算 - 条件的拼接

与 and
或 or
非 not

select * from employee where salary not in (20000,30000,3000,19000,18000,17000

4.身份运算符 - 关于null —— is null /is not null

查看岗位描述不为NULL的员工信息
select * from employee where post_comment is not null;

1.3 分组 group by

select *(或 字段名) from 表名 group by 字段名

select * from employee group by post

会把在group by后面的这个字段,也就是post字段中的每一个不同的项都保留下来,并且把值是这一项的的所有行归为一组

distinct 是基于分组完成的,所以一般去重用分组。

1.4 聚合

1.聚合:把很多行的同一个字段进行一些统计,最终的到一个结果

count(字段) — 统计这个字段有多少项

sum(字段) — 统计这个字段对应的数值的和

avg(字段) — 统计这个字段对应的数值的平均值

min(字段)

max(字段)

2.分组聚合

先分组在聚合

1.总是根据会重复的项来进行分组

2.分组总是和聚合函数一起用 最大 最小 平均 求和 有多少项

#求各个部门的人数
select post,count(*) from employee group by post;
#求公司里 男生 和女生的人数
select sex,count(id) from employee group by sex;
#求各部门的平均薪资
select post,avg(salary) from employee group by post;
#求各部门年龄最小的
select post,min(age) from employee group by post

求部门的最高薪资或者求公司的最高薪资都可以通过聚合函数取到,但是要得到对应的人,就必须通过多表查询

1.5 having 过滤

having 条件 # 过滤组 ,永远与group合用,是在group by 之后执行的。

执行顺序:总是先执行where ,再执行group by分组,所以相关先分组之后再根据分组做某些条件筛选的时候,where都用不上,只能用having来完成。

部门人数大于3的部门
select post from employee group by post having count(*) > 3
平均薪资大于10000的部门
select post from employee group by post having avg(salary) > 10000

select * from employee having age>18

注:普通的条件筛选不要用having,要使用where。

1.6 order by 查询排序

order by 某一个字段; 根据字段默认升序排列

order by 某一个字段 asc; 默认是升序 ,asc 从小到大

order by 某一个字段 desc; 指定降序排列 ,desc 从大到小

order by 第一个字段 asc,第二个字段 desc; 指定先根据第一个字段升序排列,在第一个字段相同的情况下,再根据第二个字段排列

1.7 LIMIT 限制查询的记录数

1.取前n个 : limit n == limit 0,n

  • 考试成绩的前三名
  • 入职时间最晚的前三个

2.分页 : limit m,n 从m+1开始取n个

  • 员工展示的网页
    • 18个员工
    • 每一页展示5个员工

3.limit n offset m == limit m,n 从m+1开始取n个

1.8 总结

select distinct 需要显示的列 from 表
                            where 条件
                            group by 分组
                            having 过滤组条件
                            order by 排序
                            limit (offset) 前n 条

单表查询顺序:

2. 多表查询

两张表是怎么连在一起的?

select * fr

首页 上一页 1 2 下一页 尾页 1/2/2
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇python — 表的操作(一) 下一篇Python 入门之 模块

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目