zoukankan      html  css  js  c++  java
  • 数据的增删改查

    1. 数据的增删改

    a)   增:insert into t values(值1,…….)

    b)   删:delect  from t  where 字段名=值

    c)   改:update t set 字段名=新的值 where 字段名=值

    2.关键字的作用

      distinct  去除重复数据 所有数据全都重复才算重复

      where  在逐行读取数据时的一个判断条件

      group by  对数据分组

      having   对分组后的数据进行过滤

      order by 对结果排序

      limit  指定获取数据条数

    3.单表查询

      无条件查询 :select *|字段名|四则运行|聚合函数 from t;

      完整的select 语句 语法

      select [distinct] * from 表名

      [where

      group by

      having

      order by

      limit]

    注意 在书写时 必须按照这个顺序来写  但是不代表执行顺序

    数据库伪代码

      def from():

          打开文件

      def where():

          对读取的数据进行过滤

        def group_by():

          对数据分组

       def having():

          对分组后的数据进行过滤

     def distinct():

          去除重复数据

       def order():

          排序

      def limit():

          指定获取条数

    select 语句的执行顺序

      def select(sql):

            data = from()

            data = where(data)

            data = group by(data)

            data = having(data)

            data = distinct(data)

            data = order_by(data)

            data = limit(data)

            return data;

    简单查询

        指定显示格式:

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

        select

            (

            case

            when english + math > 120 then

            concat(name," nice")

            when english + math <= 130 then

            concat(name," shit")

            end

        ) ,english,math from stu;   

    Where 条件:

             Select * from t where 条件

    group by 分组查询

      什么是分组

        把一个整体 分割为多个部分

      为什么分组

        在数据库中分组为了统计

      分组后 组里的详细记录就被隐藏起来了  不能直接查看

         可以使用group_concat(字段名)把隐藏的数据显示出来

       

    注意: 1.只有出现在group by 后面的字段 才可以被显示 其他都被影藏了

        2.聚合函数不能写在where的后面  where最先执行 它的作用硬盘读取数据并

        过滤 以为数据还没有读取完 此时不能进行统计

    什么样的字段适合用于分组

         重复性高的字段

      了解:

        在mysql 5.6中 分组后会默认显示 每组的第一条记录 这是没有意义的

         5.7不显示 因为5.7中 sql_mode中自带  ONLY_FULL_GROUP_BY

            group by 后面可以有多个分组与依据 会按照顺序执行

    order by 排序用的

      asc 表示升序 是默认的

      desc 表示降序

      by 后面可以有多个排序依据

       

    limit 限制显示条数

      limit a,b

      limit 1,5

      从1开始 到5结束 错误

      从1开始 不包含1 取5条

      使用场景:

        分页查询

    3.正表达式匹配(where 条件匹配)

      由于like只能使用% 和 _ 不太灵活

      可以将like换为 regexp 来使用正则表达式

    4.多表查询

      1.笛卡尔积查询

        select *from 表1,表n

      查询结果是:

            将坐左表中的每条记录 与右表中的每条记录都关联一遍

            a表有m条记录  b表有n条记录

            笛卡尔积结果为m * n 记录

        需要自己筛选出正确的关联关系

        select *from emp,dept where emp.dept_id = dept.id;

      2.内连接查询 就是笛卡尔积查询

            select *from emp [inner] join dept;

            select *from emp inner join dept where emp.dept_id = dept.id;

      3.左外链接查询

            select *from emp left join dept on emp.dept_id = dept.id;

            左表数据全部显示   右表只显示匹配上的

      4.右外链接查询

            select *from emp right join dept on emp.dept_id = dept.id;

            右表数据全部显示   左表只显示匹配上的

            内和外的理解   内指的是匹配上的数据  外指的是没匹配上的数据

      5.全外连接

             select *from emp full join dept on emp.dept_id = dept.id;  ##mysql不支持

           union 合并查询结果

             select *from emp left join dept on emp.dept_id = dept.id

             union

             select *from emp right join dept on emp.dept_id = dept.id;

         union 去除重复数据  只能合并字段数量相同的表

         union all 不会去除重复数据

     on 关键字 where 都是用于条件过滤  没有本质区别

        where用于单表   on 用于多表

    三表查询:用多表的理论

             例子:

        create table stu(id int primary key auto_increment,name char(10));

        create table tea(id int primary key auto_increment,name char(10));

        create table tsr(id int primary key auto_increment,t_id int,s_id int,

        foreign key(s_id) references stu(id),

        foreign key(s_id) references stu(id));

        insert into stu values(null,"张三"),(null,"李四");

        insert into tea values(null,"egon"),(null,"wer");

        insert into tsr values(null,1,1),(null,1,2),(null,2,2);

        select *from stu join tea join tsr

        on stu.id = tsr.s_id and tea.id = tsr.t_id

        where tea.name = "egon";

    多表查询套路

      1.把所有表都连起来

      2.加上连接条件

      3.如果有别的过滤条件 加上where

    5.子查询

    当一个查询的结果是另一个查询的条件 这个查询称之为子查询(内层查询)

    什么时候使用子查询

          当一次查询无法得到想要结果时  需要多次查询

    解决问题的思路

          是把一个复杂的问题 拆分为多个简单的问题

          是把一个复杂的查询 拆分为多个简单的查询

            

  • 相关阅读:
    pom.xml将jar包导入
    获取当前系统日期的前一天日期
    判断socket连接是否失效
    java读取数据,2,2,1方式读取
    笔记
    回调机制
    吧字符串按逗号分割为数组
    时间格式的转变
    java.net.SocketException四大异常解决方案
    log4j
  • 原文地址:https://www.cnblogs.com/zhouhai007/p/10004313.html
Copyright © 2011-2022 走看看