一内容回顾

  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. 创建DBA用户luna

    用system/pswd登陆sql plus,执行下面命令: 请输入用户名: system 输入口令: 连接到: Oracle Database 11g Enterprise Edition Rele ...

  2. jmeter远程调用

    jmeter版本相同 JDK版本1.7以上 脚本文件在所有机器上的路径都一致 修改远程机器jmeter bin目录下的jmeter.properties文件配置:server_port=1001 多个 ...

  3. Java面试炼金系列 (1) | 关于String类的常见面试题剖析

    Java面试炼金系列 (1) | 关于String类的常见面试题剖析 文章以及源代码已被收录到:https://github.com/mio4/Java-Gold 0x0 基础知识 1. '==' 运 ...

  4. JumpServer 架构浅解

    Jumpserver 是一款由python编写开源的跳板机(堡垒机)系统,实现了跳板机应有的功能.基于ssh协议来管理,客户端无需安装agent.完全开源,GPL授权 设计思路 设计一个跳转网关,所有 ...

  5. Java源码之HashMap的hash篇

    提到哈希,我们脑袋中立马就会闪过一个方法,就是hashCode(),没错.就是这个! 我们知道HashMap是通过 数组+链表 的结构进行数据存储的,有数组就会有索引,而HashMap内的数据要存储在 ...

  6. Gradle系列之Android Gradle基础配置

    原文发于微信公众号 jzman-blog,欢迎关注交流. 通过前面几篇文章学习了 Gradle 基础知识以及 Gradle 插件相关的知识,关于 Gradle 及其插件相关知识请先阅读下面几篇文章: ...

  7. Hadoop演进与Hadoop生态

    1.了解对比Hadoop不同版本的特性,可以用图表的形式呈现. (1)0.20.0~0.20.2: Hadoop的0.20分支非常稳定,虽然看起来有些落后,但是经过生产环境考验,是 Hadoop历史上 ...

  8. 20行代码实现,使用Tarjan算法求解强连通分量

    今天是算法数据结构专题的第36篇文章,我们一起来继续聊聊强连通分量分解的算法. 在上一篇文章当中我们分享了强连通分量分解的一个经典算法Kosaraju算法,它的核心原理是通过将图翻转,以及两次递归来实 ...

  9. web自动化(python)——selenium工具基本使用

    WebDriver基本操作 生成driver--启动浏览器 #启动谷歌浏览器,预先安装chromedrvier插件 from selenium import webdriver driver = we ...

  10. 论文阅读笔记: Multi-Perspective Sentence Similarity Modeling with Convolution Neural Networks

    论文概况 Multi-Perspective Sentence Similarity Modeling with Convolution Neural Networks是处理比较两个句子相似度的问题, ...