zoukankan      html  css  js  c++  java
  • MySQL第五讲

    内容回顾

    • 单表操作

      """
      1.配置文件先统一设置成utf8
      	s
      
      2.无论你怎么改都没有生效
      	你的机器上不止一个mysql文件
      		C有一个
      		D有一个
      
      3.百度搜索
      	show variables like '%character%'
      	
      	set ... = gbk
      	set ... = gbk
      	
      	show variables like '%character%'
      还有问题找我
      """
      select
      from 
      where
      
      # where 对整个表的筛选条件
      select * from emp where id > 3;
      模糊查询
      like
      	%  匹配任何个数的任意字符
          _  匹配单个个数的任意字符
      select * from emp where name like '%o%';
      select * from emp where name like '____';
      针对字段是null的情况 只能用is不能用=
      select * from emp where post_comment is NULL;
      
      # group by 分组
      """
      分组之后默认只能直接拿到分组的依据,其他字段无法直接获取
      select * from emp group by post;
      
      针对分组需要设置严格模式
      set global sql_mode="ONLY_FULL_GROUP_BY";
      退出客户端重新进入即可
      """
      什么时候需要分组
      	以集体为单位求相关的数据
          	每个部门、每个地区、每个国家...
              	平均年龄、薪资、身高、最大、最小、总数
      """
      聚合函数		聚合函数是分组之后才能使用
      	min
      	max
      	avg
      	sum
      	count
      select * from emp where max(salary) > 1000;  错
      select max(salary) from emp;  对  不分组默认整体就是一组
      """
      select post as '部门',max(salary) as '最高工资' from emp group by post;
      
      # 分组之后获取除分组依据以外的字段信息
      select post,group_concat(name) from emp group by post;
      select post,group_concat(name,':',salary) from emp group by post;
      
      # 分组之前也可以用拼接方法
      select concat(name,salary) from emp;
      
      
      # having分组后的再次过滤
      select post,avg(salary) from emp group by post having avg(salary) > 6000;
      
      # distinct去重
      '''必须是完全一样的数据才能去重(主键存在肯定无法去重)'''
      select distinct age from emp;
      
      # order by排序
      select * from emp order by age;  默认是升序
      select * from emp order by age asc;  升序
      select * from emp order by age desc;  降序
      select * from emp order by age asc,salary desc;
      
      # limit限制展示条数
      select * from emp limit 5;
      select * from emp limit 0,5;  第一个是起始位置、第二个是条数
      '''
      应用场景
      	1.为了节省资源
      	2.分页
      '''
      
      # regexp正则
      select * from emp where name regexp '^j.*(n|y)$';
      
    • 多表操作

      inner join  内连接
      	将两张表中有对应关系的数据拼接成一张表(只显示两张都有的)
         	select * from emp inner join dep on emp.dep_id = dep.id;
      
      left join
      	以左表为基础,没有对应的数据用NULL填充
      	select * from emp left join dep on emp.dep_id = dep.id;
      	
      right join
      	以右表为基础,没有对应的数据用NULL填充
      	select * from emp right join dep on emp.dep_id = dep.id;
      	
      union
      	将左连接和右连接生成的表再拼接一次(双方都有)
      	select * from emp left join dep on emp.dep_id = dep.id
      	union
      	select * from emp right join dep on emp.dep_id = dep.id;
      

    今日内容概要

    • 子查询的概念
    • 数据库软件navicat的使用
    • 数据库多表查询练习题(以前天作业的五张表来做)
    • python模块操作mysql数据库(后续讲python的时候再来讲)
    • 视图、触发器、存储过程、流程控制、索引(了解)

    今日内容详细

    子查询

    # 就是将一条查询语句的结果用括号括起来当作另外一条查询语句的条件
    
    # 1.查询部门是技术或者人力资源的员工信息
    """
    1.先去部门表里面将技术和人力资源对应的id号拿到
    2.再去员工表中依据步骤1查询出来的id号搜索对应的员工信息
    """
    select id from dep where name in ('技术','人力资源');
    
    select * from emp where dep_id in (200,201);
    等价
    select * from emp where dep_id in (select id from dep where name in ('技术','人力资源'));
    
    
    记住一个规律,表的查询结果可以作为其他表的查询条件,也可以通过其别名的方式把它作为一张虚拟表去跟其他表做关联查询
    

    补充知识点

    # 我想将员工的姓名、年龄、性别用:连接起来
    select concat(name,':',age,':',sex) from emp;
    如果你多个字段的连接符号是一样的话,那么可以用下面的方法
    select concat_ws(':',name,age,sex) from emp;
    
    
    # 查询平均年龄在25岁以上的部门名
    """
    1.子查询
    	分步解决问题
    	1.先操作emp表  按照dep_id分组求出平均年龄大于25岁的部门编号
    	2.根据获取到的编号去部门表里面查询出部门的名称
    """
    1.select dep_id from emp group by dep_id having avg(age) > 25;
    2.select name from dep where id in (201,202,666)
    
    select name from dep where id in (select dep_id from emp group by dep_id having avg(age) > 25);
    
    
    """
    2.连表操作
    	1.先把两张表连起来
    	2.之后直接操作连起来的一张表
    连表操作的过程中所有的表字段最好都加上表的前缀
    """
    1.select * from dep inner join emp on emp.dep_id = dep.id;
    2.select dep.name from dep inner join emp on emp.dep_id = dep.id 
    	group by dep.name
        having avg(emp.age) > 25;
        
        
        
    # exist(了解)
    EXISTS关字键字表示存在。在使用EXISTS关键字时,内层查询语句不返回查询的记录,
    而是返回一个真假值,True或False。
    当返回True时,外层查询语句将进行查询
    当返回值为False时,外层查询语句不进行查询。
    select * from emp
        where exists
        (select id from dep where id > 3);
    
    select * from emp
        where exists
        (select id from dep where id > 250);
    
    """
    百度直接搜索navicat破解版
    点击本地下载 然后根据提示点next完成安装即可
    桌面上会自动出现一个彩色的navicat图标
    	支持使用14天 过期之后卸载重新下
    	
    或者下载老版本的https://pan.baidu.com/s/1bpo5mqj
    """
    Navicat的本质其实就是内部给你封装好了对应的操作MySQL数据的sql语句
    
    
    #1. 测试+链接数据库
    #2. 新建库
    #3. 新建表,新增字段+类型+约束
    	用户表 
    		用户名
    		密码
    	录入数据
    	介绍增删改查按钮
    	
    #4. 设计表:外键
    	部门表
    		部门名
    #5.逆向数据库到模型
    	模型
    #6.建立表模型
    #7.转存数据库SQL文件
    	SQL文件一定要确保是utf8格式
    #8.新建查询语句
    	批量加注释:ctrl+?键
    	批量去注释:ctrl+shift+?键(最新版可能还是ctrl+?键)
    # 9.注释
    	# 和 --都是MySQL中的注释
    
    为了舒适的结果,眼前的坎坷路程即使再长都是值得的。
  • 相关阅读:
    辅助方法、模型、视图数据
    HTML.Label
    HTML辅助方法
    ViewBag与ViewData
    ASP.NET MVC4 View 指定视图
    ASP.NET MVC4 配置逻辑
    大部分基于MVC的Web框架所使用的一些基本原则
    MVC内置的验证属性
    高德地图多点标记自定义地图
    关于数组的去重
  • 原文地址:https://www.cnblogs.com/abudrSatan1998/p/13410351.html
Copyright © 2011-2022 走看看