一内容回顾

  insert

    insert into 表名 (字段名)  values (值)

    insert into 表名 values (有多少个字段写多少个值)

    insert into 表名 values (有多少个字段写多少个值),

              (有多少个字段写多少个值),

              (有多少个字段写多少个值),

    update

    update 表名 set  字段名 = 新的值 where 条件

    update 表名 set 字段名 = 新的值, 字段名 = 新的值 where 条件;

    

    delete

    delete from 表名 where 条件;

    

    单表查询

    select distinct 字段 from 表 where 条件

                group by 分组

                having 过滤组

                order by 排序

                limit m,n

      select  distinct 字段 from  表

      concat('name  :  ',name,' ,age : ',age)

      concat_ws('-',name,age,salary)

        egon-18-10000

      四则运算

        select age+1  from  tablename

        select salary*12 from tablename

      重命名 as

         select name as n from 表

        select  name  n  from  表

      case 语句

        case

          when  条件

            then  显示的内容

          when  条件

            then  显示的内容

          else

            显示的内容

        end

      distinct 关键字 去重功能

  where 条件

    比较运算  > < = != <> >= <=

    范围 between and/in

    like

      '%'

      '_'

    逻辑运算符 and or not

      select * from 表 where a=1 and b=2 and c=3

        and来说  有任意一个不成立就不需要继续判断了

      select * from 表 where a=1  or  b =2 or c =3

        or来说,有任意一个不成立并不影响继续判断其他条件

      select * from 表 where a not between and

  group by

    group_concat(字段)  可以显示一个分组内的所有字段内容

  having

    对分组之后的结果进行过滤

    应该和group  by连用

  聚合函数

    count

    sum

    avg

    max

    min

  order by

    默认从小到大  升序 asc

    select * from  表 order by score

    select * from  表  order by score asc

    降序排列  从大到小

    select * from  表  order by score desc

  limit

    根据前面的条件筛选出多行,取前n行

    取前n名

      order by 和 limit连用

      limit  n

    分页

      我要取所有的符合条件的项目

      但是一次性显示到页面中显示所以先返回一部分,然后再返回剩余的

      limit 0,25

      limit 25,25

      limit 50,25

  怎么用python操作mysql

  sql注入

今日内容

  多表查询

  连表查询

    先把表连起来,再查询

  子查询

    先把一个结果从一张表中查不出来,再根据结果查询另一个表

  能用子查询做的  就不用  连表

多表查询

# 一 \ 内连接
# select * from department
# inner join employee
# on department.id = employee.dep_id;
# 只会把左表和右表对应的行显示出来
# 如果左表中的department.id在employee.dep_id中没有出现,那么会抛弃这个行
# 如果右表中的employee.dep_id在department.id中没有出现,那么也会抛弃这个行 # 内连接的另一种方式 用where
# select * from department,employee
# where department.id = employee.dep_id; # 从不同的表中取字段
# select employee.name,employee.sex,department.name
# from department,employee
# where department.id = employee.dep_id; # 给表起别名,让sql更简单
# select emp.name,emp.sex,dep.name
# from department as dep,employee as emp
# where dep.id = emp.dep_id; # 给字段起别名,让显示的效果更明确
# select emp.name,emp.sex,dep.name as dep_name
# from department as dep,employee as emp
# where dep.id = emp.dep_id; # 外连接
# 二 \左外连接 : 显示左表中的所有项,和右表中所有满足拼接条件的项
# select * from department
# left join employee
# on department.id = employee.dep_id; # 三 \右外连接:显示右表中的所有项,和左表中所有满足拼接条件的项
# select * from department
# right join employee
# on department.id = employee.dep_id; # 四 \全外连接:左表和右表都完全显示出来了
# select * from department left join employee on department.id = employee.dep_id
# union all
# select * from department right join employee on department.id = employee.dep_id # 练习
# 示例一 : 找到年龄> 25的员工的姓名和部门
# select * from employee
# inner join department
# on employee.dep_id = department.id
# where age>25; # 示例一变式 : 找到alex员工的年龄和部门
# select age,department.name from employee
# inner join department
# on employee.dep_id = department.id
# where employee.name = 'alex'; # 给表重命名
# select age,dep.name from employee as emp
# inner join department as dep
# on emp.dep_id = dep.id
# where emp.name = 'alex'; # 给字段重命名引起的错误
# select employee.name as emp_name,age,department.name from employee
# inner join department
# on employee.dep_id = department.id
# where emp_name = 'alex';
# 报错,因为在select处重名名不能在where/group by/having中使用,由于mysql的词法分析顺序导致该问题 # 示例2:以内连接的方式查询employee和department表,并且以age字段的升序方式显
# select * from employee
# inner join department
# on employee.dep_id = department.id
# order by age;

多表查询_子查询

# 示例一 : 查询平均年龄在25岁以上的部门名
# 涉及到年龄 员工表
# 部门名字 部门表
# 你的结果在哪个表,那个表一定不是子查询的表 # 1. 连表查询的结果
# 先内连接得到一张大表
# select * from department
# inner join employee
# on department.id = employee.dep_id # 再根据部门分组
# select department.name from department
# inner join employee
# on department.id = employee.dep_id
# group by department.id
# having avg(age) > 25; # 2. 子查询的结果
# 先完成一部分需求,求每一个部门的人的平均年龄
# select dep_id,avg(age) from employee group by dep_id
# 再筛选出平均年龄大于25的部门
# select dep_id,avg(age) from employee group by dep_id having avg(age)>25
# 由于我们只需要部门名称,而和部门名称相关的项就只有部门id,所以我们只留下dep_id字段
# select dep_id from employee group by dep_id having avg(age)>25
# 查询部门表,找到id在上面这个查询结果内的内容
# select name from department where id in (
# select dep_id from employee group by dep_id having avg(age)>25
# )
pass
# 示例2 : 查看"技术"部员工姓名
# 结果是 : 姓名 - 员工表
# 怎么知道技术部是谁? 怎么和员工表关联?
# 如果我能知道技术部的id是多少,就可以查询了
# 1.查询技术部的id
# select id from department where name = '技术'
# 2.取id=200的所有人
# select * from employee where dep_id = (
# select id from department where name = '技术');
# 3.只取名字
# select name from employee where dep_id = (
# select id from department where name = '技术');
pass
# 示例3 :查看不足1人的部门名
# 结果是 部门名 - 部门表
# 先操作员工表
# 1.找到员工表中所有人的部门id
# select dep_id from employee group by dep_id;
# select distinct dep_id from employee;
# 2.操作部门表查看id not in 上面范围中的项目
# select name from department where id not in (
# select dep_id from employee group by dep_id); # select name from department where id not in (
# select distinct dep_id from employee);

    

day48 Pyhton 数据库Mysql 05的更多相关文章

  1. day45 Pyhton 数据库Mysql 02

    一.前期回顾 数据库 mysql的安装 配置环境 为什么要用数据库? 稳定性 一致性 并发 存取数据效率高 数据库的分类 关系型数据库 mysql oracle sqlserver 非关系型数据库 r ...

  2. day49 Pyhton 数据库Mysql 06

    多表查询 连表查询 要进行连接,那一定涉及两个表,两个表中要有关联条件才能进行连接 内连接 只有表一和表二中的连接条件都满足的时候才能显示出来 inner join on /where 条件 sele ...

  3. day46 Pyhton 数据库Mysql 03

    一内容回顾 存储引擎:主要描述的是数据存储的不同方式 innodb 支持事务\支持外键\行级锁\聚焦索引 myisam 不支持事务\不支持外键\表级锁\非聚焦索引 memory 只能在内存中存储表数据 ...

  4. day44 Pyhton 数据库Mysql

    内容回顾 什么是进程? 就是为了形容执行中的程序的一种称呼 它是操作系统中资源分配的最小单位 进程之间是数据隔离的,占用操作系统资源相对多 独立存在的 谈谈你对并发的理解 同时有多个任务需要执行,但是 ...

  5. day47 Pyhton 数据库Mysql 04

    # 表结构 # 建表 - 表的增加 # create table # 删表 - 表的删除 # drop table # 改表 - 表的修改 # alter table 表名 # rename 新表名 ...

  6. Sqoop是一款开源的工具,主要用于在HADOOP(Hive)与传统的数据库(mysql、oracle...)间进行数据的传递

    http://niuzhenxin.iteye.com/blog/1706203   Sqoop是一款开源的工具,主要用于在HADOOP(Hive)与传统的数据库(mysql.postgresql.. ...

  7. 数据库MySQL经典面试题之SQL语句

    数据库MySQL经典面试题之SQL语句 1.需要数据库表1.学生表Student(SID,Sname,Sage,Ssex) --SID 学生编号,Sname 学生姓名,Sage 出生年月,Ssex 学 ...

  8. MYSQL添加新用户 MYSQL为用户创建数据库 MYSQL为新用户分配权限

    1.新建用户 //登录MYSQL @>mysql -u root -p @>密码 //创建用户 mysql> insert into mysql.user(Host,User,Pas ...

  9. Robot Framework-DatabaseLibrary数据库(MySql)

    Robot Framework-Mac版本安装 Robot Framework-Windows版本安装 Robot Framework-工具简介及入门使用 Robot Framework-Databa ...

随机推荐

  1. STS 使用lombox.jar

    在Maven本地仓库中找到 将lombox.jar放在与STS.exe平级的目录下, 然后安装完了以后可能会出先打不开的情况.这个时候只要打开STS.ini文件. 然后修改文件保存

  2. 10 张图聊聊线程的生命周期和常用 APIs

    上一篇文章我们聊了多线程的基础内容,比如为什么要使用多线程,线程和进程之间的不同,以及创建线程的 4 种方式.本文已收录至我的 Github: https://github.com/xiaoqi666 ...

  3. vue3剖析:响应式原理——effect

    响应式原理 源码目录:https://github.com/vuejs/vue-next/tree/master/packages/reactivity 模块 ref: reactive: compu ...

  4. C#开发PACS医学影像处理系统(九):序列控件与拖拽

    1.先看结构: 创建WPF用户控件:YourTab 创建WPF用户控件:YourItem 创建选项卡时循环添加item,并设置序列缩略图到控件和异步下载的进度条, 1个病人1个或多个Study检查,1 ...

  5. Unit1:Android

    unit1 1.安卓版本 最新数据访问维基百科 2008年,android1.0 2011年,android3.0,平板失败 同年10月,android4.0,无差别使用 2014年,android5 ...

  6. CTF-WeChall-第一天

    2020.09.09 今天来了一个新平台,WeChall,从简单的开始做,才能找到自信--i春秋的题做自闭了

  7. 抓取 USB 总线LOG

    在实际工作中经常会遇到需要分析USB报文的情形.比如对比不同厂家4G/5G模块.解决实际IP over USB传输效率低下问题. 这时候如果能抓取到 USBMOM 总线的报文将会大有裨益.毕竟所有ho ...

  8. 解压gzip格式文件(包括网页)

    先上源码 参数说名: - source :gzip格式流内容. - len: gzip流长度 - dest: 解压后字符流指针 - gzip: 压缩标志,非0时解压gzip格式,否则按照zip解压 说 ...

  9. Linux下用户的创建与删除

    我们在Linux下创建用户主要有两种方式:adduser和useradd,它们的区别以及主要用法如下: adduser adduser的用法很简单,只需adduser+username即可,如下: s ...

  10. 我的Python自学之路-002 字典的知识

    '''字典是python中唯一的验证类型,采用键值对(key-value)的形式存储数据.python对key进行哈希函数运算.根据计算的结果决定value的存储地址.所以字典是无序存储的.且key必 ...