一内容回顾

  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. Java原生网络编程

    一些常见术语 编程中的Socket是应用层与TCP/IP协议族通信的中间软件抽象层,它是一组接口.在设计模式中,Socket其实就是一个门面模式,它把复杂的TCP/IP协议族隐藏在Socket接口后面 ...

  2. MySQL查询更新所有满足条件的数据

    -- 将订单表所有的状态改成1update oc_repair_preorder a inner join (select id,`status` from oc_repair_preorder) b ...

  3. LongAccumulator类的BUG——reset方法并不能保证初始值正确赋值

    LongAccumulator.reset方法并不能重置重置LongAccumulator的identity:初始值正确,使其恢复原来的初始值.当初始值为0是不会发生这个问题,而当我们设置初始值如1时 ...

  4. Blocks(POJ 3734)

    原题如下: Blocks Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 8020   Accepted: 3905 Desc ...

  5. python中的算数运算符+、-、*、/、//、%、**

    例如a=5,b=2 +    两个对象相加                              a+b=7 -    两个对象相减                              a- ...

  6. 内存管理初始化源码2:setup_arch

    PFN相关宏说明: /* kernel/include/linux/pfn.h */ PFN : Page Frame Number(物理页帧) /* * PFN_ALIGN:返回地址x所在那一页帧的 ...

  7. git如何在远程某个分支的基础上新建分支

    1.任意新建文件夹,右击git bash here $ git init(将此目录变成本地仓库) 2.$ git remote add origin 'https://git............g ...

  8. c语言汇总1

    (1--10) 1.机器语言(0,1) 汇编语言(换元法) 高级语言(人) 2.C语言由函数组成而成 main函数系统会自动启动它 3.main函数格式: int main(){ call(): re ...

  9. Ansible基础认识及安装(1)

    Ansible简介 ansible是新出现的自动化运维工具,基于Python开发,集合了众多运维工具(puppet.cfengine.chef.func.fabric)的优点,实现了批量系统配置.批量 ...

  10. 关于windows服务器的Security安全类日志的导出

    对于windows服务器,日志的分类会有很多,可以通过wevtutil el 列出 有时可能会对Security安全类日志进行审计,这里简单讲一下安全类日志的导出方法 1.直接在cmd中执行 wevt ...